如果我想将字符列表(例如“ qwertyzxc”(9个元素)(['q','w','e','r','t','y','z','x','c']
))拆分为长度为3的字符串列表,则会遇到问题。我该怎么做?
例:
输入:['q','w','e','r','t','y','z','x','c']
输出:[['q','w','e'],['r','t','y'],['z','x','c']]
谢谢,我已经坚持了很长时间
最佳答案
如果段长度固定为3,则可以通过模式匹配进行以下操作
segment :: [a] -> [[a]]
segment [] = []
segment [x] = [[x]]
segment [x,y] = [[x,y]]
segment (x:y:z:rest) = [x,y,z] : segment rest
*Main> segment [1,2,3,4,5,6,7,8,9]
[[1,2,3],[4,5,6],[7,8,9]]
关于string - 您如何将一个字符串分成所有长度为3的字符串列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44186214/