Home > Back-end >  How does the Lambda function work on lists in Haskell?
How does the Lambda function work on lists in Haskell?

Time:02-02

I have this line of code and don't quite understand why the outcome is [False, False]

i :: [Bool]
i = filter (\x-> (not x)) [False,False,True,True,True] 

I thought it would come out to [True,True,True].

Would really appreciate a short explanation of the Outcome here. Thanks in advance

CodePudding user response:

According to the documentation,

filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate, i.e. [...]:

>>> filter odd [1, 2, 3]
[1,3]

The expression that defines i is essentially saying:

Filter the booleans x for which not x is True from the list [False, False, True, True, True]

  • For the first entry, not False is True, so it is retained.
  • For the second entry, not False is again True, so this is retained as well.
  • For the remaining three entries, not True is False, so these entries are dropped.

The result is [False, False].

CodePudding user response:

Because filter keeps the elements where the predicate is true. These elements are now False, False.

  • Related