haskell

How can i use haskell to check if a list contains the values in a tuple


I am writing some functions for graphs in Haskell, and I want to check if a list of integers, such as

[1,4,5,7]

contains vertices that make an edge, which i have represented as a tuple, like so

(1,5)

I'm trying to take a function that takes the list and the tuple, and in this case would return true, because the list contains a 1 and a 5. The main issue i am having is that I'm really not sure how to search a list in Haskell. Is there a function that takes a list of type [a] and a value of type a, and returns a Bool, depending on whether [a] contains a?


Solution

  • There is a function to check whether a value is in a list,

    elem :: Eq a => a -> [a] -> Bool
    

    Using that, your function is easily defined.

    containsEdge :: [Int] -> (Int,Int) -> Bool
    xs `containsEdge` (a,b) = (a `elem` xs) && (b `elem` xs)