Haskell tail function for empty lists

I have a problem with a function that should only return the tail of a list. These functions are myTail and should give useful results, even if the input is an empty list.

I want to understand all three ways: pattern matching, protected equation, and conditional expressions

it works:

> myTail_pat :: [a] -> [a] > myTail_pat (x:xs) = xs > myTail_pat [] = [] 

But this:

 > myTail_guard (x:xs) | null xs = [] > | otherwise = xs 

gives me an error: Program error: template match failure: myTail_guard [] How can I declare a function without templates?

Thanks.

+7
list haskell tail
source share
2 answers

Sample x:xs does not match empty lists. You will need to do:

 myTail_guard xs | null xs = [] | otherwise = tail xs 
+15
source share

Fall 1 is safe

 drop 1 [] -- result: [] 
+3
source share

All Articles