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 whichnot x
isTrue
from the list[False, False, True, True, True]
- For the first entry,
not False
isTrue
, so it is retained. - For the second entry,
not False
is againTrue
, so this is retained as well. - For the remaining three entries,
not True
isFalse
, 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
.