1. Junior Coders
Join Date
Oct 2008
Posts
10
Thanks
0
Thanked 0 Times in 0 Posts

Hi there,

I'm stuggling to get my head around a problem and was wondering if someone was able to help.

I have a list of tuples (each tuple with two ints), and a seperate tuple consisting of two ints. When any of the ints from the first list matches either of the ints from the lone tuple, I want the whole tuple (two ints) returned.

That's a terrible explaination, so here's an example to clarify:

[(2,2),(6,5),(6,6),(2,5),(3,1)] ..... the list of tuples

(5,3)..... the lone tuple

So with these inputs, the function I need would return (6,5) and (3,1).

Any thoughts?

2. Senior Coders
Join Date
May 2007
Location
USA
Posts
373
Thanks
2
Thanked 4 Times in 4 Posts
Note: All the tests do the same thing.
Code:
```test1 (x, y) = filter \$ \(w, z) -> x == w || x == z || y == w || y == z

test2 (x, y) = filter \$ \(w, z) -> x `elem` [w, z] || y `elem` [w, z]

test3 (x, y) = filter \$ \(w, z) -> any (`elem` [w, z]) [x, y]

test4 p = filter \$ any (`elem` pairToList p) .  pairToList

pairToList (x, y) = [x, y]```
Code:
```> test1 (5,3) [(2,2),(6,5),(6,6),(2,5),(3,1),(5,1)]
[(6,5),(2,5),(3,1),(5,1)]

> test2 (5,3) [(2,2),(6,5),(6,6),(2,5),(3,1),(5,1)]
[(6,5),(2,5),(3,1),(5,1)]

> test3 (5,3) [(2,2),(6,5),(6,6),(2,5),(3,1),(5,1)]
[(6,5),(2,5),(3,1),(5,1)]

> test4 (5,3) [(2,2),(6,5),(6,6),(2,5),(3,1),(5,1)]
[(6,5),(2,5),(3,1),(5,1)]```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•