[Haskell-beginners] Need better explanation of the 'flipThree' example in LYAH

Francesco Ariis fa-ml at ariis.it
Mon Sep 17 00:09:18 UTC 2018


Hello Olumide,

On Mon, Sep 17, 2018 at 12:15:58AM +0100, Sola Aina wrote:
> flatten( fmap (\a -> ( flatten( fmap ( \b -> return [a,b] ) coin ) ) coin )
> 
> Is this how to proceed and how am I to simplify this expression?

I get a slightly different final expression:

    flatten (fmap (\a -> flatten (fmap (\b -> return [a,b]) coin)) coin)
    -- notice the lack of `(` before the second `flatten`

This could be rewritten as:

    flatMap (\a -> flatMap (\b -> return [a,b]) coin) coin

    -- flatMap :: (a1 -> Prob a2) -> Prob a1 -> Prob a2
    -- flatMap f x = flatten (fmap f x)

which is a bit easier on the eyes. This expression cannot be simplified
any further if we don't bring to the table the definition of `coin`!


More information about the Beginners mailing list