[Haskell-beginners] Copy record with mutations?

Chaddaï Fouché chaddai.fouche at gmail.com
Thu Apr 14 20:14:04 CEST 2011


On Wed, Apr 13, 2011 at 5:22 PM, Andrew n marshall
<andrew.n.marshall at gmail.com> wrote:
>
> Is there a syntax or function to copy a record, but with select fields
> populated with new values?
> For example, extending LYAH's Car
>
> let stang67 = Car {company="Ford", model="Mustang", year=1967}
> let stang68 = stang67 `mutate` Car {year=1968}
>

The classic solution is simply :

> let stang68 = stang67  {year=1968}

but that is not very convenient (not at all in fact) when for instance
you have several nested records or you want to base the "new" value of
a field on its "old" value. Thus was invented the concept of
"functional references" or "lenses" which extend this mechanism by a
more complete and convenient protocol to copy a record with
modification on some of its field.
See

http://hackage.haskell.org/package/fclabels

http://www.cis.upenn.edu/~bcpierce/papers/newlenses-popl.pdf

There are plenty of lenses packages on Hackage, I don't know which are
more used, maybe some that uses them could help here ?

-- 
Jedaï



More information about the Beginners mailing list