Task

Define two associative arrays, where one represents the following "base" data:





 Key 
 Value


 "name" 
 "Rocket Skates"


 "price" 
 12.75


 "color" 
 "yellow"

And the other represents "update" data:





 Key 
 Value


 "price" 
 15.25


 "color" 
 "red"


 "year" 
 1974

Merge these into a new associative array that contains every key found in either of the source ones. Each key should map to the value in the second (update) table if that exists, or else to the value in the first (base) table. If possible, do this in a way that does not mutate the original two associative arrays. Obviously this should be done in a way that would work for any data, not just the specific data given here, but in this example the result should be:





 Key 
 Value


 "name" 
 "Rocket Skates"


 "price" 
 15.25


 "color" 
 "red"


 "year" 
 1974



data Item = Item
  { name :: Maybe String
  , price :: Maybe Float
  , color :: Maybe String
  , year :: Maybe Int
  } deriving (Show)
 
itemFromMerge :: Item -> Item -> Item
itemFromMerge (Item n p c y) (Item n1 p1 c1 y1) =
  Item (maybe n pure n1) (maybe p pure p1) (maybe c pure c1) (maybe y pure y1)
 
main :: IO ()
main =
  print $
  itemFromMerge
    (Item (Just "Rocket Skates") (Just 12.75) (Just "yellow") Nothing)
    (Item Nothing (Just 15.25) (Just "red") (Just 1974))