Haskell Declare an empty list, but which is not really empty?

I am new to Haskell and will play a little. I created a recursive function with protection. 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 dataset created using 10 people that I use in this method. When I tried this function, it gave all the right people, but after that I got an error not related to the exhaustive templates: ["Lise","Jaap","Elle","Ebba"*** Exception: D:\...:(44,1)-(47,77): Non-exhaustive patterns in function filterAge

I learned that he never reaches the first guard. So I played a little and found out something really strange (in my opinion):

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

Now my main question is: why (x:xs) == []returns False?

If someone has a better way for me to make a function that will be great, but that is not very important.

Thanks in advance!

EDIT

Willem Van Onsem Lambda.xy.x . , :

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

.

+4
2

:

data [] a = [] | a : [a]

, : [] (x:xs) , ( ).

(x:xs) - : x. xs ( [a]), x a, "" . let , (x:xs), .

, . , . :

filterAge :: [Person] -> [String]
filterAge []     = [] -- empty list case
filterAge (x:xs) -- first guard dropped
    | (getAge x) < 30 || (getAge x) > 40  = [] ++ filterAge xs
    | otherwise                           = [getName x] ++ filterAge xs

, , , , , , () .

, :

  • getAge, , where, ;
  • [] ++ somelist - somelist: ;
  • [element] ++ somelist is element : somelist, .

, filterAge :

filterAge :: [Person] -> [String]
filterAge []     = [] -- empty list case
filterAge (x:xs) | age < 30 || age > 40  = filterAge xs
                 | otherwise             = getName x : filterAge xs
    where age = getAge x

, ( ) -Wincomplete-patterns ( ), Haskell , , , .

+10

"" ,

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

, x xs, (x:xs) [] . , .

?

*Main> (x:xs) == [] -- (2)

, (x:xs) []. ; , , , - False ( ). , (_:_) [] , False .

, , x xs, ; , (x:xs) [] (1) .

+4

Source: https://habr.com/ru/post/1680791/


All Articles