Mark Provan
Mark Provan

Reputation: 1061

How do I print out each item in a list of lists in Haskell?

I have a list like:

[[1,2,3,4,5],[6,7,8,9]]

I'm trying to do it using list comprehension and have got as far as:

each_in_lists x = [show y | y <- x]

where x is the list of lists.

Upvotes: 4

Views: 5023

Answers (4)

Luis Casillas
Luis Casillas

Reputation: 30237

The other answers so far share what I think is a big flaw: it's always good to split a problem into smaller subproblems, and they don't do it. In this case, the applicable subproblems are:

  1. Flatten a nested list
  2. Print the contents of a flat list

The standard function that does #1 is concat:

>>> concat [[1,2,3,4,5],[6,7,8,9]]
[1,2,3,4,5,6,7,8,9]

Now, the easiest way to print the elements is mapM_ print, as other answers have suggested:

>>> mapM_ print (concat [[1,2,3,4,5],[6,7,8,9]])
1
2
3
4
5
6
7
8
9

This generalizes to all sorts of cases, BTW: in Haskell, the straightforward way of sequentially iterating over the elements of some structure is to convert it to a flat list, and then process the list.

Upvotes: 4

Davorak
Davorak

Reputation: 7444

Extending Satvik mapM_ answer some I think it makes the code more composable if instead of using a nested structure like:

> mapM_ (mapM_ print) x 

a structure that takes advantage of function composition.

> (mapM_.mapM_) print x

The advantage of this is that you can then simply extend this to list of list of list, [[[a]]] by just adding another mapM_ .

> (mapM_.mapM_.mapM_) print x

This composition concept can be taken even further to include tuples [[(a,a)]] and data structures. This is done in lens. An example print all the elements of [(a,a)] would be:

mapMOf_ (traverse.both) print [(1,2),(3,4),(5,6)]

Upvotes: 3

dharga
dharga

Reputation: 2217

Assuming you're trying to flatten the list and want to use list comprehension as much as possible, I think the following is as close as you'll get.

x = [[1,2,3,4,5],[6,7,8,9]]

sequence [print z | y <- x, z <- y]

Upvotes: 1

Satvik
Satvik

Reputation: 11218

Are you trying to convert it to string

> let x = [[1,2,3,4,5],[6,7,8,9]] 
> [show a | y <- x, a <- y]
["1","2","3","4","5","6","7","8","9"] 

If you are trying to print, use print. print uses the show instance of the elements to convert it to string and then uses putStrLn.

> mapM_ (mapM_ print) x                          
1                                                                                   
2                                                                                   
3                                                                                   
4                                                                                   
5                                                                                   
6                                                                                   
7                                                                                   
8                                                                                   
9 

Upvotes: 7

Related Questions