Prelude> mapM putStrLn ["a", "b"]
a
b
[(),()]
Prelude> mapM_ putStrLn ["a", "b"]
a
b
Why first version shows third line and second does not and where does third line comes from. I would not expect it.
If you put the mapM
version in a standalone program, compile it with ghc
, and run it, you don't get a third line from it, either:
$ cat demo.hs
main = mapM putStrLn [ "a", "b" ]
$ ghc demo.hs
$ ./demo
a
b
$
That [(),()]
you see in ghci
is just the return value of the mapM
call; ghci
automatically displays the value of every expression you enter. (This is why ghci
is called a Read-Evaluate-Print Loop, or REPL; the "Print" part is what you're seeing here.)
While mapM
creates a list containing the return value of every putStrLn
call (so you get one ()
for each element in the list), mapM_
discards those return values and returns IO ()
, which ghci
doesn't bother to display. So you don't see an extra line from ghci
in that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With