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

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

    As hammar pointed out, join is the "monadic" way to flatten a list. You can use the do-Notation as well to write easily flatten functions of several levels:

    flatten xsss = do xss <- xsss
                      xs <- xss
                      x <- xs
                      return x
    

提交回复
热议问题