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?
CodePudding user response:
As it seems you are defining your function entirely within ghci, the most likely source of your problem is that each line is introducing a new myTail
function. In this case when you call myTail []
the current myTail
function does not handle empty lists, so you get an exception.
ghci> myTail [] = []
ghci> myTail (_:xs) = xs
ghci> myTail []
*** Exception: <interactive>:3:1-18: Non-exhaustive patterns in function myTail
You can define a function across multiple lines using :{
and :}
.
ghci> :{
ghci| myTail [] = []
ghci| myTail (_:xs) = xs
ghci| :}
ghci> myTail []
[]
It is worth additionally noting, though, that it doesn't make much sense for a function that returns the tail of a list to return an empty list when given an empty list when in fact an empty list doesn't have a tail.
You may want to use Maybe
to handle this possibility.
ghci> :{
ghci| myTail [] = Nothing
ghci| myTail (_:xs) = Just xs
ghci| :}
ghci> :t myTail
myTail :: [a] -> Maybe [a]
ghci> myTail []
Nothing
ghci> myTail [1, 2, 3]
Just [2,3]