1
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.

2
  • i mean how can i make it work. Commented Jun 3, 2011 at 13:35
  • 2
    I believe the question is: Is there a way to do partial matches on tuples like you would do full matches with the elem function. Commented Jun 3, 2011 at 13:45

2 Answers 2

8

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)]]
Sign up to request clarification or add additional context in comments.

2 Comments

can you give an example usage?
matchElem (r,s,_) = any (\(a,b,_) -> a == r && b == s)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.