Monad vs sequential function calls


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"?

Show source
| haskell   | monads   | scala   2016-12-28 02:12 1 Answers

Answers to Monad vs sequential function calls ( 1 )

  1. 2016-12-28 03:12

    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

Leave a reply to - Monad vs sequential function calls

◀ Go back