SplitAt 3然后继续分裂 [英] SplitAt 3 and then keep on splitting

查看:145
本文介绍了SplitAt 3然后继续分裂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我曾尝试使用 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屋!

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