Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2003-10-21 03:43:14

"Brian McNamara" <lorgon_at_[hidden]> wrote in message
> [...]
> I'd be very interested to see how someone who doesn't know
> monads goes about writing a generic C++ implementation of
> mothersPaternalGrandfather", which works for all the cases
> (Sheep have {exactly 1, 0 or 1, any number} of each type of
> parent). That example, while contrived, captures much of the
> essence of monads.

template <typename SheepT>
SheepT mother( SheepT s ) throw( NoSuchAncestor );
template <typename SheepT>
SheepT father( SheepT s ) throw( NoSuchAncestor );

template <typename SheepT>
SheepT mothersPaternalGrandfather( SheepT s )
        throw (NoSuchAncestor )
    return father(father(mother(s)));

exactly 1: SheepT == Sheep
0 or 1: SheepT == variant<Sheep, noSheep>
any number: SheepT == vector<Sheep>

But the devil is in the details. I get the feeling that even
though my solution effectively does the same thing as
yours, that it misses the point of monads. The problem
may be that FP and IP are so different that the focus is
placed differently, and thus implementations aren't really
comparable. After all, the real work is done in the mother()
and father() functions, and we have left those unspecified.

Part of the problem is that this example is so contrived,
I'm having a hard time linking it to something useful. Your
parser example is a step in the right direction, but it seems
to me that there are a lot of ways to implement what you
suggest that don't involve monads at all. It seems that
the parser example suffers from being too high-level, while
monads seem to be a solution that is supposed to be
useful at a low level as well.

What might help is to look at some real world use-cases,
like the I/O monads mentioned in the tutorial. If we could,
say compare some basic I/O monad with the analogous
functionality in, say, iostreams, that might illustrate a
practical benefit of monads that even a C++ coder can
relate to. ;)


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

Boost list run by bdawes at, gregod at, cpdaniel at, john at