[Haskell-beginners] Basic experimenting with monads

Patrick LeBoutillier patrick.leboutillier at gmail.com
Wed Mar 4 17:45:00 EST 2009

Hi all,

I'm pretty much a haskell newbie (reading RWH at chapter 15) and I'm
trying to understand monads better, more specifically the mechanics of
>>= chains.

For example, take the following code:

skip :: IO a -> IO String
skip a = return "?"

main = do
    putStrLn "1"
    n <- skip $ do
        putStrLn "2"
        putStrLn "3"
    putStrLn $ "skipped " ++ n
    putStrLn "4"

When I run it, it prints, as expected:

skipped ?

My understanding is that what is really "passed" to the skip function
is something like this:

putStrLn "2" >> putStrLn "3"

Here is my question: is it possible for the skip function to return
the number of actions that were skipped? In other words, is it
possible to go through the chain above without executing the actions
but counting them instead?

It seems to me it should be possible to do this, in a way similar to
what happens when executing the Maybe monad (a return value of Nothing
triggers the return of other Nothing values, but the full length of
the chain is still executed (RWH, p.38)).

Am I on the right track here?

Thanks a lot,


Patrick LeBoutillier
Rosemère, Québec, Canada

More information about the Beginners mailing list