[Haskell-beginners] Why does sequence (map print [1, 2, 3, 4, 5]) produce [(), (), (), (), ()] at the end?

David McBride toad3k at gmail.com
Mon Feb 1 17:39:50 UTC 2016


map :: (a -> b) -> [a] -> [b]
print :: Show a => a -> IO ()

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

print takes a showable a and creates a procedure that prints it out (IO ()).
So therefore map print causes each element in the array to become a
procedure that prints out its element, thus the return value is [IO()].

Note that it has not actually printed them out.  It merely has an array of
as yet unexecuted actions.  To print them you'd go like sequence (map
print) [1,2,3], or better yet, sequence_ which returns () instead of [()]

On Mon, Feb 1, 2016 at 12:28 PM, Olumide <50295 at web.de> wrote:

> Hello list,
>
> The question says it all.
>
> BTW, I'm studying LYH and I'm on the chapter on IO. The book offers and
> explanation but its not very clear -- to me at least.
>
> Thanks,
>
> - Olumide
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160201/90753424/attachment.html>


More information about the Beginners mailing list