I am declaring a new type for memory and then use a function to update it. The program compiles and works fine when I add values to the list, but if my list is empty I get the error:
*** Exception: Non-exhaustive patterns in function update
Here is my code, if you can please help me:
type Name = [Char]
type Memory = [(Name, Integer)]
update :: Name -> Integer -> Memory -> Memory
update n x (h:t)
| fst h == n = (n, x) : t
| h : t == [] = [(n, x)]
| otherwise = h : update n x t
This is because your code doesn’t cover the empty list case.
In particular this:
h:t == []will never evaluate toTrue.h:tis a pattern which will only match a non-empty list: it bindshto the head of the list andtto the rest of the list.So your function needs to handle three cases: