El Guapo

El Guapo El Guapo

Niner since 2007


  • Brian Beckman: Don't fear the Monad

    I'm a C++ programmer who has been interested in learning what's behind all the recent "fuss" about functional languages.

    I no longer "fear" monads, but I am now quite perplexed about why they're even worth discussing! I am willing to accept that I'm totally missing the boat here, but please do help me understand...

    Let's see if I can paraphrase one example from the video:

    we have two functions:

    f: a -> Ma
    g: a -> Ma

    and we wish to compose them, but alas one cannot pipe output from one straight into the other. So we define a "bind operator" that lets us do this; we use some fancy formatting and rules to describe a lamda function that inside it deals with the different input/output types in order to get the two functions interacting happily.

    Er, do we need functional programming to do this?

    It seems like in any language/paradigm that I can very simply create a new "ordinary" function:

    h: Ma -> a

    responsible for stripping off the M and compose the two quite quickly. Is this approach not equivalent? What makes "bind" so special? For example:

    struct M { int data; foo meta; }

    M f( int ) { ... }
    M g( int ) { ... }
    int h( M ) {... }

    M compose_fg( int x ) { return g( h ( f( x ) ) ); }

    So where have I gone wrong Smiley ?