Is join useful for every monad?

Georgi Lyubenov godzbanebane at gmail.com
Mon Oct 14 08:36:46 UTC 2019


In general, if you want to *dynamically generate* actions depending on the
result of an earlier action you will always encounter join/(>>=).
For example (with ReadPrec/Parser):
I want to first parse a character, and then parse the same character two
more times.
numberAndThenThatManyAs = join (fmap (\c -> satisfy (==c) *> satisfy (==c))
char)

Of note:
* The example is contrived for simplicity's sake, but you do really need a
Monad (and hence join) to perform stuff like this in general. A more
practical example would be parsing command-line options that depend on
previous options.
* Obviously it's way more humane to write this with do-syntax. (or (>>=) or
something) - do { c <- char; satisfy (==c); satisfy (==c) }
* I'm not actually sure whether you need a Monad in this situation, maybe
you could get away with just selectives
<http://hackage.haskell.org/package/selective-0.3>

=======
Georgi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20191014/5a4e9496/attachment.html>


More information about the Libraries mailing list