|
|
|
|
|
by alipang
4807 days ago
|
|
You can't overload = or ; in Haskell. What you can do is to easily write code over some sort of 'boxed' values, where the type of the box is given by type signatures, and more importantly is almost always quite clear from context. In this case, think of Maybe as a box containing one or zero instances of a type. For Maybe do x <- maybeAnInt
y <- maybeAnotherInt
return (x+y)
Lists are 'boxed' values containing any number of elements do x <- [1,2]
y <- [10,20]
return (x+y)
The monadic semantics for lists means this returns the sum of each combination of values, namely [11,21,12,22]. This is essentially like a database join, which is why a monadic structure was used for LINQ.For Promises do x <- intPromise
y <- anotherIntPromise
return (x+y)
This returns a new promise containing the sum of the result of two promises instead of using callbacks.Essentially all these types live in a Maybe/List/Promise box. There are many more examples. The nice thing about monads is that the semantics of how these things work is abstract enough to allow a variety of interpretations, but constrained enough (by the Monad laws) that you get a nice intuition of how things work after using a few different instances. |
|