Return a list of Integer values that is not within a range of numbers in an existing list:

≯℡__Kan透↙ 提交于 2019-12-04 06:19:27

问题


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]


来源:https://stackoverflow.com/questions/6163640/return-a-list-of-integer-values-that-is-not-within-a-range-of-numbers-in-an-exis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!