简体   繁体   中英

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

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.

You can check if any of the elements of the sublists contain X with:

any (elem X) [[X, X, X], [O, O, O]]

or with elem as infix operator:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM