【发布时间】:2021-08-23 13:26:49
【问题描述】:
multipleLists 函数需要从列表中删除给定值并返回列表列表。这些子列表对每个元素进行分组,直到它达到需要删除的值。发生这种情况时,会启动一个新的子列表。
input: multipleLists 'b' "abcabc"
expected output: ["a","ca","c"]
actual output: ["a","c","a","c"]
input: multipleLists 1 [0,1,2,3,4,1,3,4]
expected output: [[0],[2,3,4],[3,4]]
actual output: [[0],[2],[3],[4],[3],[4]]
我认为otherwise 的案例有问题,但我有点卡住了。
这是我的代码:
multipleLists :: Eq a => a -> [a] -> [[a]]
multipleLists value list = case list of
[] -> []
[x] -> [[x]]
x:xs
| value == x -> multipleLists value xs
| otherwise -> [x] : (multipleLists value xs)
【问题讨论】:
-
multipleLists 1 [1]的值是多少? -
一个空列表 - []
-
你的第一个例子错了吗?那不应该是
["a","ca","c"]
标签: list haskell recursion sublist