Haskell Programming Problem: module Store ( Store, initial, -- Store a b value,
ID: 3761851 • Letter: H
Question
Haskell Programming Problem: module Store ( Store, initial, -- Store a b value, -- Store a b -> a -> b update, -- Store a b -> a -> b -> Store a b -- DON'T FORGET TO EXPORT merge ) where newtype Store a b = Store (a -> Maybe b) initial :: Store a b initial = Store (_ -> Nothing) value :: Store a b -> a -> Maybe b value (Store sto) v = sto v update :: Eq a => Store a b -> a -> b -> Store a b update (Store sto) key value = Store (key' -> if key == key' then (Just value) else sto key') -- example store :: Store Char Integer store = update (update (update initial 'a' 1) 'b' 2) 'c' 3
Implement the function to the above code merge :: Store a b -> Store a b -> Store a b that takes two stores as input and produces the corresponding "merged" store as output. Note that there are four possibilities: a key could be contained in a) none of the two stores b) only the first store c) only the second store d) both stores. In case d), the first store has priority, that is, the merged store should contain the value from the first store.
Explanation / Answer
b) only the first store