25

I was playing around Maybe and Either monad types (Chaining, applying conditional functions according to returned value, also returning error message which chained function has failed etc.). So it seemes to me like we can achieve same and more things that Maybe does by using Either monad. So my question is where the practical or conceptual difference between those ?

1
  • 2
    I'm stunned this isn't a dupe. Great question. Mar 2, 2017 at 15:06

3 Answers 3

24

You are of course right that Maybe a is isomorphic to Either Unit a. The thing is that they are often semantically used to denote different things, a bit like the difference between returning null and throwing a NoSuchElementException:

  • Nothing/None denotes the "expected" missing of something, while
  • Left e denotes an error in getting it, for whatever reason.

That said, we might even combine the two to something like:

query :: Either DBError (Maybe String)

where we express both the possibility of a missing value (a DB NULL) and an error in the connection, the DBMS, or whatever (not saying that there aren't better designs, but you get the point).

Sometimes, the border is fluid; for saveHead :: [a] -> Maybe a, we could say that the expected possibility of the error is encoded in the intent of the function, while something like saveDivide might be encoded as Float -> Float -> Either FPError Float or Float -> Float -> Maybe Float, depending on the use case (again, just some stupid examples...).

If in doubt, the best option is probably to use a custom result ADT with semantic encoding (like data QueryResult = Success String | Null | Failure DBError), and to prefer Maybe to simple cases where it is "traditionally expected" (a subjective point, which however will be mostly OK if you gain experience).

17

@phg's answer is great. I will chime in with something that helped clear it up for me when I was learning them:

  • Maybe is one (value) or none – ie, you have a value or you have nothing
  • Either is a logical disjunction, but you always have at least one (value) - ie, you have one or the other, but not both.

Maybe is great for things like where you may or may not have a value - for example looking for an item in a list. if the list contains it, we get (Just x) otherwise we get Nothing

Either is the perfect representation of a branch in your code - it's going to go one way or the other; Left or Right. We use a mnemonic to remember it: Right is the right (correct) way; Left is the wrong way (Error). This is not it's only use of course, but definitely the most common.

I know the differences might seem subtle at first, but really they're suitable for very different things.

4
  • 1
    For mentioning logical disjunction you get the + 1. This is very approachable answer for someone new to FP. Mar 2, 2017 at 15:08
  • Thanks Jared. Your feedback means a lot to me ^_^
    – Mulan
    Mar 2, 2017 at 16:07
  • "for example looking for an item in a list" I think this example makes it even harder to really understand the difference? That still looks like Either. You either get an element or nothing
    – Farid
    Jan 26 at 8:32
  • @Farid no, Either means either value A or value B. Maybe means maybe a value or no value at all.
    – Mulan
    Jan 26 at 18:03
1

Well, you see, we can put this to the extreme by saying that all product types can be represented by just 2-tuples and all non-recursive sum types by Either. To additionally represent recursive types, we need a fixpoint type.

For example, why have 4-tuples (a,b,c,d) when we could as well write (a, (b, (c,d))) or (((a,b), c), d) ?

Or why have lists, when the following works as well?

data Y f = Y (f (Y f))

type List a = Y ((,) (Either () a))


nil = Y (Left (), undefined)

cons a as = Y (Right a, as)

infixr 4 cons

numbers = 1 `cons` 2 `cons` 3 `cons` nil

-- this is like foldl

reduce f z (Y (Left (), _)) = z
reduce f z (Y (Right x, xs)) = reduce f (f z x) xs


total = reduce (+) 0 numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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