Grouping a list into lists of n elements in Haskell

前端 未结 4 2157
刺人心
刺人心 2020-12-06 09:33

Is there an operation on lists in library that makes groups of n elements? For example: n=3

groupInto 3 [1,2,3,4,5,6,7,8,9] = [[1,2,3],[4,5,6],[7,8,9]]
         


        
4条回答
  •  旧时难觅i
    2020-12-06 09:58

    This function, among other similar ones, can be found in the popular split package.

    > import Data.List.Split
    > chunksOf 3 [1,2,3,4,5,6,7,8,9]
    [[1,2,3],[4,5,6],[7,8,9]]
    

提交回复
热议问题