SplitAt 3然后继续分裂 [英] SplitAt 3 and then keep on splitting
问题描述
我曾尝试使用 splitAt
,但是我想在Haskell中每隔3个字母就会分割一个字符串。我必须重复此操作,直到字符串每3个字母一起分开。
有没有办法做到这一点?
Eg
WEAREDISCOVERED
会变成 [WEA, RED,ISC,OVE,RED]
everyThree :: [a] - > [[a]]
everyThree [] = []
everyThree xs = begin:everyThree保留
其中(begin,remain)= splitAt 3 xs
所以这适用于任何类型的包括字符串的列表。所以如果列表是空的,我们只需返回一个空列表。否则,我们 splitAt 3
。幸运的是, splitAt
很聪明,如果剩下的列表太短,它将只返回一个,两个或无。然后我们将该开始字符串附加到剩余的 everyThree
列表的头部。
I want to split a string in Haskell every 3 letters along.
I have tried using splitAt
but I would have to repeat this until the string is split every 3 letters along.
Is there a way to do this?
E.g
"WEAREDISCOVERED"
would become ["WEA","RED","ISC","OVE","RED"]
everyThree :: [a] -> [[a]]
everyThree [] = []
everyThree xs = begin : everyThree remain
where (begin, remain) = splitAt 3 xs
So this will work for any type of list including strings. So if the list is empty we just return an empty list. Otherwise we splitAt 3
. Fortunately, splitAt
is smart and will return just one, two, or none if the remaining list is too short. Then we append that beginning string to the head of the remaining everyThree
list.
这篇关于SplitAt 3然后继续分裂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!