[英]Haskell `elem` in nested list
I want to check for an element in a nested list.我想检查嵌套列表中的元素。
I already tried several ways but I will always get the wrong result or some errors.我已经尝试了几种方法,但我总是会得到错误的结果或一些错误。
[X] `elem` [[X,X,X],[O,O,O]]
returns False but should be True返回 False 但应该为 True
X `elem` [[X,X,X],[O,O,O]]
throws a error, that types can not be matched.抛出错误,即类型无法匹配。
Do I miss something here?我在这里想念什么吗?
The elements of the list are sublists, and there is no [X]
sublist in the list.列表的元素是子列表,列表中没有[X]
子列表。
You can check if any of the elements of the sublists contain X
with:您可以检查子列表的任何元素是否包含X
:
any (elem X) [[X, X, X], [O, O, O]]
or with elem
as infix operator:或使用elem
作为中缀运算符:
any (X `elem`) [[X, X, X], [O, O, O]]
but these are semantically completely identical.但这些在语义上是完全相同的。
These will check if for any of the sublists (here [X, X, X]
and [O, O, O]
), X
is an element of these lists.这些将检查任何子列表(此处为[X, X, X]
和[O, O, O]
), X
是否是这些列表的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.