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!
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.
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 !!")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With