Mic*_*ron 1 haskell functional-programming function list purely-functional
在Haskell中,如何将x个数字列表更改为n个n个数字列表?
第一个子列表的编号从第一到第十,第二个列表从第11到第20 ......
myFunction :: [Int] - > [[Int]]
还有就是chunksOf在功能上Data.List.Split:
chunksOf 2 [0, 1, 2, 3] -- [[0, 1], [2, 3]]
Run Code Online (Sandbox Code Playgroud)
另外,我们已经splitAt在prelude,与chunksOf可以轻松实现:
chunksOf :: Int -> [a] -> [[a]]
chunksOf n [] = []
chunksOf n xs = let (as, bs) = splitAt n xs in as : chunksOf n bs
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |