[Haskell-beginners] Loop

Marcin Mrotek marcin.jan.mrotek at gmail.com
Fri May 1 13:08:26 UTC 2015


Hello,

First of all, putStrLn has type IO (), so b is pretty useless in your
code, it's going to have type (), and the only value of this type is
(). The simplest way to perform some action based on a list and
discard the results is mapM_:

mapM_ (\_ -> putStrLn "Test") [1..4]

though if you discard the list's elements too, you can use sequence_
and replicate instead:

sequence_ . replicate 4 $ putStrLn "Test"

As a side note, if you actually want to combine monads (like list and
IO) you can use monad transformers. The ListT type from transformers
library has some issues as far as I know, but you can use the one from
pipes, for example. Have a look at:
http://www.haskellforall.com/2014/11/how-to-build-library-agnostic-streaming.html

Best regards,
Marcin Mrotek


More information about the Beginners mailing list