Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskell - turning list of lists into one list

If I have a list of lists, say [[1,2,3],[1,2,3],[1,2,3]], is there any way in Haskell to turn this into just 1 list, like [1,2,3,1,2,3,1,2,3]?

Thanks in advance!

like image 264
user1670032 Avatar asked Oct 08 '12 19:10

user1670032


People also ask

How do you reverse a list in Haskell?

Algorithm. Haskell is a functional programming language where we can leverage recursion to reverse a list. This can be done by adding the first element ( x) of the list at the end of the returning list, and calling the recursive function with the list ( x s xs xs) that does not contain x.

How do I display lists in Haskell?

Example #1print("Demo to show list in Haskell !!") let list1 = [100, 50, 235, 167, 345, 909, 675, 20] let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] let list3 = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6] let list4 = [5, 10, 15, 20, 15, 30, 35, 40] let list5 = [123.4, 567.9, 56.0, 3.9, 76.9] print("printing list element !!")


1 Answers

Concat does what you'd like:

concat [[1,2,3],[1,2,3],[1,2,3]]

To find these sorts of functions in the future, you can use hoogle http://www.haskell.org/hoogle/

You can search for a type - your required function is [[Int]] -> [Int], so you could do this search. The top function is concat.

I should mention that in fact

concat :: [[a]] -> [a]

So it works on any list of lists, and you could also quite happily search hoogle with that type instead. Hoogle's smart enough to understand which types are appropriately close to what you asked for, though.

like image 89
AndrewC Avatar answered Nov 16 '22 03:11

AndrewC