开发者

Haskell element of list

elem (1,2,3) [(1,2,3)] -> works (true)

elem (1,2,_) [(1,2,3)开发者_StackOverflow中文版] -> doesnt work (want it return true as well)

What Im trying to do is if the first two elements of tuple matches one in the list return true.


You can use the prelude function any to find out whether at least one element in a list meets a given condition (the condition in this case being "it matches the pattern (1, 2, _)").

An example for this case would be:

any (\x -> case x of (1,2,_) -> True; _ -> False) [(1,2,3),(4,5,6)]

Or a bit more concisely:

or [True | (1,2,x) <- [(1,2,3),(4,5,6)]]


You can use elem if convert the triples to pairs first:

elem (1,2) $ map (\(a,b,_) -> (a,b)) [(1,2,3),(4,5,6)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜