Tuesday, March 28, 2006

How do you say "maybe"?

  • Haskell, ML: Maybe T, T option
  • Scheme: either T or #f
  • Java, C#: either an ordinary T or null
  • ML, Scheme, Java, C#: either a value or an exception
  • Prolog: success or failure


Anonymous said...

it's actually (T option) in ML :) -- well what you actually *say* is (SOME V) or (NONE), (T option) is the type.


Dave Herman said...

Ack! Of course. Fixed.

Duncan said...


I've used ternary logic in Smalltalk before, using the values 'true', 'false' and 'nil'.

^ aPerson isHappy
ifNil: ['Maybe?.'];
ifTrue: ['It's true']
ifFalse: ['It's false'].

Notice that this consists of 2 message sends (cascaded to the same receiver by the semicolon syntax), they are:

1) ifNil:
2) ifTrue:ifFalse:

KSM said...

"Either T or #f" is not so good in Scheme if T can be #f. Better is "(list T) or #f", or your favorite box of T or your favorite non-box cookie (empty list, zero element vector).

But the other way to say maybe is "pair of continuations", sometimes even in direct style.

scruzia said...

In scala:

val foo: Option[T]

Here's the API doc.

Dave Herman said...

Fine, but that's no different from T option in ML. My list was intended to demonstrate implementations of the pattern that are different in interesting ways.