Results 1 to 2 of 2

Thread: Basic Haskell problem

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

    Default Basic Haskell problem

    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. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

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

Bookmarks

Posting Permissions

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