Haskell/Understanding monads/Solutions/IO

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Print a list of arbitrary values[edit | edit source]

The function map would print out every value from the list, but would produce [IO ()]. We'd thus need to chain sequence $ map, which is exactly how the function mapM is defined. Because we are not collecting the results of print but focus only on its actions, we can discard them with mapM_.

printList :: Show a => [a] -> IO ()
printList = mapM_ print