Haskell Declare empty list, but which is actually not empty?

Viewed 5254

I'm new to Haskell and are playing around a bit. I created a recursive function with guards. See function below:

filterAge :: [Person] -> [String]
filterAge (x:xs)
 | (x:xs) == []                        = []
 | (getAge x) < 30 || (getAge x) > 40  = [] ++ filterAge xs
 | otherwise                           = [getName x] ++ filterAge xs

I have a data set created with 10 Persons which I use in this method. When I tried this function it gave all the right people, but after that it got a Non-exhaustive patterns error: ["Lise","Jaap","Elle","Ebba"*** Exception: D:\...:(44,1)-(47,77): Non-exhaustive patterns in function filterAge

I found out that it never reaches the first guard. So I played around a bit and found out something really strange (in my opinion):

*Main> let (x:xs) = []
*Main> (x:xs) == []
False

Now my main question is: Why does (x:xs) == [] return False?

If anyone has a better way for me of doing the function that'd be great, but it's not very important.

Thanks in advance!

EDIT

Thanks to Willem Van Onsem and Lambda.xy.x I got a quick answer to my question. This resulted in the following function which works perfectly:

filterAge :: [Person] -> [String]
filterAge []                           = []
filterAge (x:xs)
 | (getAge x) < 30 || (getAge x) > 40  = [] ++ filterAge xs
 | otherwise                           = [getName x] ++ filterAge xs

But for the best version you'd have to check the answer of Willem Van Onsem.

2 Answers
Related