[Haskell-beginners] Basic experimenting with monads
alexander.dunlap at gmail.com
Wed Mar 4 20:03:45 EST 2009
On Wed, Mar 4, 2009 at 3:58 PM, Conrad Meyer <konrad at tylerc.org> wrote:
> 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).
> Conrad Meyer <konrad at tylerc.org>
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.
More information about the Beginners