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

后端 未结 7 1251
小鲜肉
小鲜肉 2020-12-02 07:03

How can I flatten a nested list like this:

[1, 2, 3, 4] == flatten [[[1,2],[3]],[[4]]]
相关标签:
7条回答
  • 2020-12-02 07:46

    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.

    0 讨论(0)
提交回复
热议问题