2

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

duplode
  • 33,731
  • 7
  • 79
  • 150
  • Your questions are not very clearly worded. Can you revise your post and make your questions easier to understand? –  Dec 28 '16 at 01:54
  • Near-duplicate: [*Monads, composition and the order of computation*](http://stackoverflow.com/q/41310361/2751851) – duplode Dec 28 '16 at 01:59
  • Also relevant: [the second answer to *Why do we need monads?*](http://stackoverflow.com/a/28141248/2751851) – duplode Dec 28 '16 at 02:05
  • 2
    Actually sequencing is possible for more general F-algebras. Think `foldl`. Monads are significant precisely because they can define more well-behaving T-algebras (like Kleisli algebras). – mnish Dec 28 '16 at 08:25
  • 1
    So your instinct (whatever that is) is perhaps correct, sequencing of operations alone does not characterize monads. – mnish Dec 28 '16 at 08:29
  • 1
    In "transparent" monads, where you can access the underlying definition (e.g. state, reader, writer, continuation, list, maybe, ...), you could do everything without mentioning monads at all, and only using plain functions. The monad abstraction is however useful since it is a common ground for all these cases. It's more about convenience than necessity. In opaque monads like IO or ST, the library does not allow sequencing but through the monad interface, though. – chi Dec 28 '16 at 10:25
  • @chi: But if I remember correctly, IO and ST are just opaque state monads hiding the internal states. Hiding states is equally possible with F-algebras of the form F(F(a))->F(a). – mnish Dec 28 '16 at 10:36
  • 2
    So if the true motivation of this question was 'are monads really necessary just for sequencing?' then the answer is certainly no. A better question would be 'what kind of trouble would arise if IO were not a monad?' – mnish Dec 28 '16 at 10:42
  • @mnish "A better question would be 'what kind of trouble would arise if IO were not a monad?'" -- A recent duplicate for this interpretation of the question is [*Why wrap an IO result in IO Monad*](http://stackoverflow.com/q/41096040/2751851) – duplode Dec 28 '16 at 18:11

1 Answers1

3

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
duplode
  • 33,731
  • 7
  • 79
  • 150