【发布时间】:2022-01-04 01:08:12
【问题描述】:
将列表划分为子列表:将相同的值添加到一个子列表中,将不同的值添加到另一个子列表中。它需要使用foldr 通过列表的一轮完成,而不使用++。
例如:
ghci> splitList [1,2,2,4,5,7,7,8,9]
[[1],[2,2],[4,5],[7,7],[8,9]]
我做了类似的事情:
splitList :: [Int] -> [[Int]]
splitList = getRes . foldr func5 ([],False,0) where
func5 c ([],_,_) = ([[c]],False,c)
func5 c (y@(x:xs), b, l) | c == l = ((c:x):xs, False, c)
| b = ((c:x):xs, False, c)
| otherwise = ([c]:y, True, c)
getRes :: ([[Int]], Bool, Int) -> [[Int]]
getRes (a,_,_) = a
但它并没有像我预期的那样工作......
ghci> splitList [1,2,2,4,5,7,7,8,9]
[[1],[2,2],[4,5],[7,7,8],[9]]
【问题讨论】: