
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} such that company and model are "inherited".

On 13 April 2011 17:22, Andrew n marshall
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}
let stang67 = Car {company="Ford", model="Mustang", year=1967} let stang68 = stang67 { year = 1968 }

Yeah, that's basically it. Keeping in mind that Haskell's data structures are immutable "let stang68 = stang67" allows to use symbol "stang68" as a new Car. Whether an actual copy is made does not matter: even if both "stang"s point to the same structure you're not allowed to change its values. El mié, 13-04-2011 a las 17:27 +0200, Christopher Done escribió:
On 13 April 2011 17:22, Andrew n marshall
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}
let stang67 = Car {company="Ford", model="Mustang", year=1967} let stang68 = stang67 { year = 1968 } _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, Apr 13, 2011 at 5:22 PM, Andrew n marshall
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ï

Thanks for the follow-up. While I'm still getting used to reading/writing the numerous new operators/infix notations, I've started looking into the Data.Accessor package. Is this similar in scope to the lenses you're referring to? On 4/14/11 11:14 AM, Chaddaï Fouché wrote:
On Wed, Apr 13, 2011 at 5:22 PM, Andrew n marshall
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 ?

On Fri, Apr 22, 2011 at 2:37 AM, Andrew n marshall
Thanks for the follow-up.
You're welcome.
While I'm still getting used to reading/writing the numerous new operators/infix notations, I've started looking into the Data.Accessor package. Is this similar in scope to the lenses you're referring to?
Absolutely, in data-accessor description you even find :
For similar packages see lenses and fclabel.
Data.Accessor.Accessors are lenses (or functional references, whatever you want to call them...). -- Jedaï
participants (4)
-
Andrew n marshall
-
Chaddaï Fouché
-
Christopher Done
-
MAN