Basic experimenting with monads

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: 1 skipped ? 4 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 -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

On Wednesday 04 March 2009 02:45:00 pm Patrick LeBoutillier wrote:
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?
I am also a beginner (so I am probably wrong), but I think actually the (>>)
function turns two IO actions into one, and your function only recieves that
one IO action. I am not sure if that can be decomposed or not (probably not).
Regards,
--
Conrad Meyer

On Wed, Mar 4, 2009 at 3:58 PM, Conrad Meyer
On Wednesday 04 March 2009 02:45:00 pm Patrick LeBoutillier wrote:
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?
I am also a beginner (so I am probably wrong), but I think actually the (>>) function turns two IO actions into one, and your function only recieves that one IO action. I am not sure if that can be decomposed or not (probably not).
Regards, -- Conrad Meyer
Actually, you're exactly right. The type of (>>) is (>>) :: IO a -> IO b -> IO b. Since there is no primitive IO b -> Int (which counts the number of actions in the IO action), there's no way to count them. However, there is a way to solve the original problem. If you define a list of the actions (let a = [putStrLn "2", putStrLn "3"]) then you can use length to count the number of actions in the list and sequence_ (:: [IO a] -> IO (), from Control.Monad) to run them. Alex
participants (3)
-
Alexander Dunlap
-
Conrad Meyer
-
Patrick LeBoutillier