Reputation:
The Wikipedia article on monads says:
Purely functional programs can use monads to structure procedures that include sequenced operations like those found in structured programming.
Not looking for an (another) monad tutorial here. Just please give an example of monad making sequenced operations possible when just running one function and then another is not enough. Is it somehow connected with those function calls being lazy per functional language specifications? Why sequential run of not-interchanged functions needs any "wrapper"?
Upvotes: 2
Views: 254
Reputation:
The Haskell Monad Tutorial clearly shows an example of sequencing function calls in a monadic fashion:
type Sheep = ...
father :: Sheep -> Maybe Sheep
father = ...
mother :: Sheep -> Maybe Sheep
mother = ...
-- comb is a combinator for sequencing operations that return Maybe
comb :: Maybe a -> (a -> Maybe b) -> Maybe b
comb Nothing _ = Nothing
comb (Just x) f = f x
-- now we can use `comb` to build complicated sequences
mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = (Just s) `comb` mother `comb` father `comb` father
Upvotes: 3