[Haskell-beginners] Problems with IO actions
Jason Dusek
jason.dusek at gmail.com
Thu Jul 2 18:31:38 EDT 2009
2009/07/01 Adam Bergmark <adam at edea.se>:
> * I'm still a little unsure of why mapM_, sequence and the
> like are necessary, could someone please explain this, or
> point me somewhere where I can read up on it?
Say we have a list of `IO` actions (type `[IO a]`). We can't
run lists directly; the runtime runs `IO` action (type `IO
something`). It's easy to do the translation but we'd rather
not do it all the time: here's an easy recursive definition of
`sequence`:
sequence ops = recurse [] ops
where
recurse acc [ ] = return $ reverse acc
recurse acc (op:rest) = do
result <- op
sequence' (result:acc) rest
The other operations are further conveniences that can be
built up from `sequence`.
> * Why does this problem occur when there is only one action to
> perform?
Do you close the handle?
--
Jason Dusek
More information about the Beginners
mailing list