Still learning Haskell with Get Programming with Haskell.
In the lesson 7 there is the following exercise:
The
tail
function in Haskell returns an error when called on an empty list. ModifymyTail
so that it does handle the case of an empty list by returning the empty list.
Where myTail
is defined as follows:
myTail (_:xs) = xs
In the very same lesson, we are told that it is possible to check empty lists parameters with pattern matching. Here is an example from the book:
isEmpty [] = True
isEmpty _ = False
So here it is what I thought would do the trick
myTail [] = []
myTail (_:xs) = xs
However, when I use this function with an empty list it throws an exception:
ghci> myTail []
*** Exception: <interactive>:2:1-17: Non-exhaustive patterns in function myTail
What is wrong?