Return a list of Integer values that is not within

2019-03-04 18:12发布

问题:

I have a list of values:

[0,7,4,5,3,1,4,5,5,1,7,0,7,7,0]

and would like to return any values that are not in the range of [1..8]

(i.e. I would like to return (from the above example) the elements 2, 6 and 8 in the form

[2,6,8]

)

I seem to have trouble putting this together into a function. I know that notElem would work well here but am not sure on how to apply the list [1..8] to the list of values shown above to get the elements shown just then.

回答1:

Use filter to keep elements that satisfies a condition.

Prelude> filter (`notElem` theBigListOfValues) [1..8]
[2,6,8]

Or just take the complement using the (\\) operator.

Prelude> import Data.List
Prelude Data.List> [1..8] \\ theBigListOfValues
[2,6,8]