0
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?

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
CgirlS
  • 1
  • Note that since the equality operator `==` works on `Int`, it also automatically works on `[Int]`. So while not directly answering your question, the easiest way to implement `compare1` is just `compare1 = (==)`. – bame Nov 02 '20 at 11:42

1 Answers1

0

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`
cocreature
  • 801
  • 5
  • 5