[Haskell-beginners] Basic experimenting with monads
patrick.leboutillier at gmail.com
Wed Mar 4 17:45:00 EST 2009
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
For example, take the following code:
skip :: IO a -> IO String
skip a = return "?"
main = do
n <- skip $ do
putStrLn $ "skipped " ++ n
When I run it, it prints, as expected:
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,
Rosemère, Québec, Canada
More information about the Beginners