Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was doing a presentation on F# and was discussing the Option type when someone in the audience asked me if the Option type is F#'s implementation of the maybe monad. I know that's not the case but I did want to ask how the two concepts are related. I mean it seems to me that an option type might be the result of the operation of a maybe monad but I'm not even sure of that.

Would someone elucidate the relationship between the maybe monad and the option type in those functional languages which support it?

share|improve this question
1  
Just curious, but how do you know that's not the case? They look very similar to me. –  Matt Fenwick Jun 22 '12 at 13:44
    
That's why I'm asking. –  Onorio Catenacci Jun 22 '12 at 13:53
add comment

1 Answer

up vote 7 down vote accepted

Reading the documentation on F#'s Option type, it looks like it does behave pretty much exactly like the Maybe type in Haskell, in that it can model either 'nothing' (None in F#, Nothing in Haskell), or a value of its argument type (Some in F#, Just in Haskell).

In Haskell, however, Maybe is also a monad, and the plumbing is such that it allows for calculations on Maybe values, early-returning Nothing if any of the variables in the calculation is Nothing. Used this way, Maybe is a simple error handler (or rather, error-ignoring device), and the fact that it is a monad allows moving the boilerplate out of the way. Look at this wikipedia article for a nice concise example. I don't think Option supports this kind of monadic usage (in fact, I'm wondering whether there is any explicit concept of a monad in F# at all). If you want this behavior in .NET, I guess you'd use Option.Value for all your arguments, and wrap the whole calculation in a try / catch on NullReferenceException.

So, while Option is similar to the Maybe type, it is not an equivalent to the Maybe monad.

share|improve this answer
    
Thanks for the lucid explanation. Frankly I always considered they weren't the same (one's a type the other's a monad) but I wasn't altogether clear on how one related to the other. –  Onorio Catenacci Jun 22 '12 at 13:55
    
BTW--there is a monad type of concept in F#; it's called a computation expression. –  Onorio Catenacci Jun 22 '12 at 13:56
    
@OnorioCatenacci: Ah, OK. I haven't had an awful lot of F# exposure, so excuse me. Anyway, in Haskell, monads are types, more specifically, types of the Monad typeclass. Maybe is a plain old data type, but it also happens to implement Monad to provide this 'special' behavior. –  tdammers Jun 22 '12 at 14:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.