我想创建将字符串拆分为子字符串列表的函数,其中每个子字符串的长度为k:
*Main> split_string_to_kmers "some_text" 3
["som","ome","me_","e_t","_te","tex","ext"]
这是我的解决方案:
split_string_to_kmers s k = split_string_to_kmers_helper s k []
where split_string_to_kmers_helper [] k acc = acc
split_string_to_kmers_helper s k acc
| length s >= k = split_string_to_kmers_helper (tail s) k (acc ++ [(take k s)])
| otherwise = acc
我只是想知道是否有办法重写我的代码,因此它会更具特定于haskell。
答案 0 :(得分:2)
我猜这有点不同。
import Data.List (tails)
mySplit :: String -> Int -> [String]
mySplit str k = filter (\s -> length s == k) $ map (take k) (tails str)
通过组合过滤器和地图,您可以提高效率。但这取决于你。
答案 1 :(得分:2)
下一个简单的解决方案(不同于列表的尾部):
import Data.List.Split(chop)
splitRepN n = chop (\xs -> (take n xs,tail xs))
我们有下一个结果:
> splitRepN 3 "some_text"
["som","ome","me_","e_t","_te","tex","ext","xt","t"]
我们缩短了尾巴以获得完整的解决方案:
splitRepN' n = takeWhile ((== n). length) . splitRepN n
> splitRepN' 3 "some_text"
["som","ome","me_","e_t","_te","tex","ext"]