I was reading the purescript wiki and found following section which explains do
in terms of >>=
.
What does >>=
mean?
Do notation
The do keyword introduces simple syntactic sugar for monadic expressions.
Here is an example, using the monad for the
Maybe
type:maybeSum :: Maybe Number -> Maybe Number -> Maybe Number maybeSum a b = do n <- a m <- b let result = n + m return result
maybeSum
takes two values of typeMaybe Number
and returns their sum if neither number isNothing
.When using do notation, there must be a corresponding instance of the Monad type class for the return type. Statements can have the following form:
a <- x
which desugars tox >>= \a -> ...
x
which desugars tox >>= \_ -> ...
or just x if this is the last statement.- A let binding
let a = x
. Note the lack of thein
keyword.The example
maybeSum desugars to
::maybeSum a b = a >>= \n -> b >>= \m -> let result = n + m in return result