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

6 comments:

  1. Anonymous8:16 PM

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

    -marius

    ReplyDelete
  2. Ack! Of course. Fixed.

    ReplyDelete
  3. Hey,

    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:

    ReplyDelete
  4. "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.

    ReplyDelete
  5. In scala:

    val foo: Option[T]

    Here's the API doc.

    ReplyDelete
  6. 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.

    ReplyDelete