mapM putStrLn ["a", "b"] a b [(),()] Prelude> mapM_ putStrLn ["a", "b"] a b Why ...">

MapM putStrLn ["a", "b"] why does it show three lines?

Prelude> mapM putStrLn ["a", "b"] a b [(),()] Prelude> mapM_ putStrLn ["a", "b"] a b 

Why does the first version show the third line, and the second does not and where does the third line come from. I would not expect this.

+4
source share
1 answer

If you put the version of mapM in a standalone program, compile it with ghc and run it, you wonโ€™t get the third line from it:

 $ cat demo.hs main = mapM putStrLn [ "a", "b" ] $ ghc demo.hs $ ./demo a b $ 

That [(),()] that you see in ghci is just the return value of the mapM call; ghci automatically displays the value of each expression entered. (This is why ghci is called the Read-Evaluate-Print Loop or REPL loop, the Print part is what you see here.)

While mapM creates a list containing the return value of each call to putStrLn (so you get one () for each item in the list), mapM_ discards the return values โ€‹โ€‹and returns IO () that ghci does not bother displaying itself. So you do not see the extra line from ghci in this case.

+17
source

Source: https://habr.com/ru/post/1444938/


All Articles