thetux4
thetux4

Reputation: 1633

Haskell element of list

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

elem (1,2,_) [(1,2,3)] -> 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.

Upvotes: 1

Views: 1316

Answers (2)

Landei
Landei

Reputation: 54574

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)]

Upvotes: 1

sepp2k
sepp2k

Reputation: 370082

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)]]

Upvotes: 8

Related Questions