Is there a function to flatten a nested list of elements?

Yes, it’s concat from the Standard Prelude, given by

concat :: [[a]] -> [a]
concat xss = foldr (++) [] xss

If you want to turn [[[a]]] into [a], you must use it twice:

Prelude> (concat . concat) [[[1,2],[3]],[[4]]]
[1,2,3,4]

Leave a Comment