Hacker News new | past | comments | ask | show | jobs | submit login

Did you misunderstand?

    do x <- a
       y <- b
       return $ x + y
can result in Nothing if either a or b is Nothing, but

    (fromMaybe 0 a) + (fromMaybe 0 b)
will always result in a number, with 0 being used in place of Nothing. If only one of them is Nothing, you'll get the value of the other, the Nothing having been treated as 0.



No, I didn't misunderstand. 'Nothing' and 0 are not interchangeable.


You did misunderstand: you took a friendly illustration of an alternative way to use Maybe as some kind of absolutist degree. Well, put down your sword. Much of the time when you have Maybe you also have a sensible default value and you want to use that rather than live the rest of your life inside Maybe. fromMaybe is just a handy way to deal with that case.


> No, I didn't misunderstand. 'Nothing' and 0 are not interchangeable.

Nobody said they were. That's idiomatic Haskell for something like this in Java when unboxing Integers:

  return (a != null ? a : 0) + (b != null ? b : 0);
As you can see, there is no adding Nothing to integers.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: