0
compare1:[Int] -> Book
Compare1[x]  =([x] == [x])

Test1 = scenario do

Debug(compare1 [11,12])

上面的代码有什么问题为什么daml:44-1-30:Non-exhaustive patterns in function compare1会出现错误?

4

1 回答 1

0

让我们看看这里的关键线:

compare1 [x] = [x] == [x]

在等号的左边你有模式匹配[x]。这匹配单个元素列表,并将该单个元素绑定到 name x。那么错误告诉您没有处理所有其他情况(空列表和具有多个元素的列表)。

要解决这个问题,您有两个选择,要么将模式匹配更改为变量xs(或任何其他名称)。无论元素数量如何,这将匹配任何列表并将列表绑定到 name xs

compare1 xs = …

或者,您可以使用 2 个模式匹配来覆盖列表为空且列表具有 1 个或多个元素的情况:

compare1 [] = … -- do something for empty lists
compare1 (x :: xs) = … -- do something with the head of the list bound to `x` and the tail bound to `xs`
于 2020-11-02T07:46:24.523 回答