我可以使用库函数重写吗?
chunks :: ([a] -> ([a], [a])) -> [a] -> [[a]]
chunks f [] = []
chunks f xs = case c of
[] -> cs
ys -> ys : cs
where
(c, rest) = f xs
cs = chunks f rest
如果我给它一个类似于span
的函数,它总是从列表中取出至少一个元素,它将使用列表并返回一个列表,其中列出了每次函数被中断的元素的子列表被叫了。
答案 0 :(得分:8)
这看起来非常像展开:
http://hackage.haskell.org/package/base-4.8.0.0/docs/Data-List.html#v:unfoldr
也许像(未经测试):
chunks f xs = unfoldr f' xs
where f' [] = Nothing
f' xs' = Just $ f xs'