How can I flatten a nested list like this:
[1, 2, 3, 4] == flatten [[[1,2],[3]],[[4]]]
An arbitrarily nested list can be approximated by a Data.Tree
, which can be flattened by the appropriately named function flatten
.
I say approximated because Data.Tree
allows a data item to be attached to every node, not just the leaves. However, you could create a Data.Tree (Maybe a)
, and attach Nothing
to the body nodes, and flatten with catMaybes . flatten
.