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

后端 未结 7 1269
小鲜肉
小鲜肉 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:37

    You can remove one level of nesting using concat, and consequently you can apply n levels of nesting by applying concat n times.

    It is not possible to write a function which removes an arbitrary level of nestings, as it is not possible to express the type of a function, which takes an arbitrarily nested list and returns a flat list, using Haskell's type system (using the list datatype that is - you can write your own datatype for arbitrarily nested lists and write a flatten function for that).

提交回复
热议问题