title: Haskell records update
body: <p>I would like to update a record with external (streams of) name/value pairs. The record's fields have different types. </p>
  
  <p>That's how I did it, is there a more elegant Haskell way to do this?</p>
  
  <pre><code>data MyRec = MyRec {field1 :: String, field2 :: Int, field3 :: Bool} deriving Show
  defRec = MyRec {field1 = "", field2 = 0, field3 = False}
  
  singleUpdate :: String -&gt; MyRec -&gt; MyRec  -&gt; MyRec
  singleUpdate "field1" urec rec = rec {field1 = field1 urec}
  singleUpdate "field2" urec rec = rec {field2 = field2 urec}
  singleUpdate "field3" urec rec = rec {field3 = field3 urec}
  singleUpdate _ _ rec = rec
  
  update :: [(String, MyRec)] -&gt; MyRec -&gt; MyRec
  update flds rec = foldl (flip (uncurry singleUpdate)) rec flds
  
  nameVals = [("field1",defRec {field1 = "foo"}), ("field3",defRec {field3 = True})]
  updtdRec   = update nameVals defRec
  </code></pre>
  
  <p>updtdRec returns <code>MyRec {field1 = "foo", field2 = 0, field3 = True}</code></p>
  
  <p>Is there a more compact way, closer to something like this : <code>update field val = MyRec {field = val}</code> (this is just pseudo-code), which works for all fields, without having to specify the function implementation for each field?</p>