I want to implement a function which takes a key k :: Either a b, and a list of pairs. I want the keys to have two shapes. One of the shape Left l, which will perform a lookup on the left half of the pairs, and one for the right half. Both shall return the other half of the first matching pair in the list. If no match is found, the function should return Nothing.
An instance of the Output with example list:
namesAges = [("Felix", 45), ("Grace", 25), ("Hans", 57), ("Ivy", 25)]
bidirectionalLookup (Left "Grace") namesAges == Just (Right 25)
bidirectionalLookup (Right 57) namesAges == Just (Left "Hans")
I've successfully defined the functions for lookupRhs as well as lookupLhs but I am not able to combine them in my "bidirectionalLookup" function. What function form do I use? ITE, case-of, pattern-matching?
This is one version of my attempts. I have many modifications but none gave me any results. I have a feeling that I am on the wrong track.
namesAges = [("Felix", 45), ("Grace", 25), ("Hans", 57), ("Ivy", 25)]
lookupLhs :: Eq a => a -> [(a, b)] -> Maybe b
lookupLhs x ((l, r) : namesAges) = if x == l then Just r else lookupLhs x namesAges
lookupRhs :: Eq b => b -> [(a, b)] -> Maybe a
lookupRhs x ((l, r) : namesAges) = if x == r then Just l else lookupRhs x namesAges
bidirectionalLookup :: (Eq b, Eq a) => Either a b -> [(a, b)] -> Maybe (Either a b)
bidirectionalLookup (Left x) namesAges = lookupLhs x
bidirectionalLookup (Right x) namesAges = lookupRhs x
bidirectionalLookup _ _ = Nothing
I am aware that this is beginner level and that I may be completely off track (or have the answer right in front of my nose for that matter), still any kind of help will be greatly appreciated.