[Haskell-cafe] record update

Luke Palmer lrpalmer at gmail.com
Tue Sep 14 19:17:45 EDT 2010


On Tue, Sep 14, 2010 at 1:31 PM, Jonathan Geddes
<geddes.jonathan at gmail.com> wrote:
> With these extensions, couldn't I write the following?
>>someUpdate :: MyRecord -> MyRecord
>>someUpdate myRecord@(MyRecord{..}) = let
>>     { field1 = f field1
>>     , field2 = g field2
>>     , field3 = h filed3
>>     } in myRecord{..}

No, those are recursive let bindings!  If f = (1:), then field1 = [1,1,1,1...]

As Conrad suggests, use:

   someUpdate myRecord@(MyRecord{..}) = myRecord
      { field1 = f field1
      , field2 = f field2
      , field3 = f field3
      }

The reason this works is that "field1" in "field1 = " is not a real
scoped variable, but rather an identifier for a field in the record.
It's all somewhat subtle...

Luke


More information about the Haskell-Cafe mailing list