Reputation: 1
compare1:[Int] -> Book
Compare1[x] =([x] == [x])
Test1 = scenario do
Debug(compare1 [11,12])
What's wrong with the above code why the error daml:44-1-30:Non-exhaustive patterns in function compare1
is appearing?
Upvotes: 0
Views: 136
Reputation: 811
Let’s look at the crucial line here:
compare1 [x] = [x] == [x]
On the left side of the equal sign you have the pattern match [x]
. This only matches a single element list and will bind that single element to the name x
. So what the error is telling you that all other cases are not handled (empty lists and lists with more than one element).
To fix that you have two options, either you change the pattern match to just a variable xs
(or any other name). That will match any list regardless of the number of elements and bind the list to the name xs
.
compare1 xs = …
Alternatively, you can use 2 pattern matches to cover the case where the list is empty and the list has 1 or more elements:
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`
Upvotes: 0