Log in

View Full Version : Basic Haskell problem



R.B.
10-25-2008, 02:16 PM
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?

Trinithis
10-25-2008, 06:19 PM
Note: All the tests do the same thing.


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]




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