在haskell中细分列表 [英] Subdividing a list in haskell

查看:93
本文介绍了在haskell中细分列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如
$ b $如何将一个列表分组成一个长度相等的小列表(除了最后一个子列表)? b

 子列表3 [1,2,3,4,5,6,7,8]  - > [[1,2,3],[4,5,6],[7,8]] 
子列表2 [4,1,6,1,7,3,5,3] - > [[4,1],[6,1],[7,3],[5,3]]


解决方案

如果你想坚持前奏,你可以使用 splitAt

  splitEvery _ [] = [] 
splitEvery n list = first:(splitEvery n rest)
where
(first,rest)= splitAt n列表


How can I group a list into smaller lists of equal length (except last sublist) in haskell?

E.g.

sublist 3 [1,2,3,4,5,6,7,8] -> [[1,2,3],[4,5,6],[7,8]]
sublist 2 [4,1,6,1,7,3,5,3] -> [[4,1],[6,1],[7,3],[5,3]]

解决方案

If you want to stick to prelude, you can pull this off using splitAt.

splitEvery _ [] = []
splitEvery n list = first : (splitEvery n rest)
  where
    (first,rest) = splitAt n list

这篇关于在haskell中细分列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆