返回不在现有列表中的数字范围内的整数值列表:

发布于 2024-11-10 10:06:14 字数 299 浏览 10 评论 0原文

我有一个值列表:

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

并且想返回不在 [1..8] 范围内的任何值

(即我想返回(从上面的示例)元素 2 、 6 和 8 的形式

[2,6,8]

我似乎很难将其组合成一个函数。我知道 notElem 在这里可以很好地工作,但不确定如何将列表 [1..8] 应用于上面显示的值列表以获取当时显示的元素。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-11-17 10:06:15

使用filter 保留满足条件的元素。

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

或者只是采取 使用 (\\) 运算符补码

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

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文