
Haskell's record syntax is quite nice, for a number of reasons. However, suppose I have some record: data Foobar = Foobar {foo1, foo2, foo3...} Now suppose that foo3 :: [Int], and I want to prepend 5 to it. I end up having to write something like let v1 = v0 {foo3 = 5 : (foo3 v0)} If the field name isn't "foo" but something more descriptive, and the transformation to be applied to it is more intricate, you end up with quite a bit of code. In summary, record syntax gives you a nice way of replacing the value of one field with something else, but no easy way to *modify* the existing value somehow. Does anybody know of a way around this? Is there some trick I'm not seeing? Is there an extension or proposal that fixes this?

Take a look at Data.Accessor on hackage:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor-tem...
-- ryan
On Sun, Mar 29, 2009 at 2:13 AM, Andrew Coppin
Haskell's record syntax is quite nice, for a number of reasons. However, suppose I have some record:
data Foobar = Foobar {foo1, foo2, foo3...}
Now suppose that foo3 :: [Int], and I want to prepend 5 to it. I end up having to write something like
let v1 = v0 {foo3 = 5 : (foo3 v0)}
If the field name isn't "foo" but something more descriptive, and the transformation to be applied to it is more intricate, you end up with quite a bit of code.
In summary, record syntax gives you a nice way of replacing the value of one field with something else, but no easy way to *modify* the existing value somehow.
Does anybody know of a way around this? Is there some trick I'm not seeing? Is there an extension or proposal that fixes this?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Ryan Ingram wrote:
Take a look at Data.Accessor on hackage:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor-tem...
So, to summarise, it provides a datatype to encapsulate a pair of get/set functions, some infix sugar for using it, and some TH for autogenerating said data. Is that about right? I'll have a go at trying this later... Thanks.

There is a page for extensible records in the wiki:
http://www.haskell.org/haskellwiki/Extensible_record
Haskell's records system has many insuficiencies.
Some libraries (see grapefruit or HaskellDB) encode records as
classes, but although some proposals Haskell still lacks a good
implementation for extensible and updatable records.
On Sun, Mar 29, 2009 at 10:41 AM, Andrew Coppin
Ryan Ingram wrote:
Take a look at Data.Accessor on hackage:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor-tem...
So, to summarise, it provides a datatype to encapsulate a pair of get/set functions, some infix sugar for using it, and some TH for autogenerating said data. Is that about right?
I'll have a go at trying this later... Thanks.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- www.di.uminho.pt/~hpacheco

On Sun, 29 Mar 2009, Andrew Coppin wrote:
Ryan Ingram wrote:
Take a look at Data.Accessor on hackage:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor-tem...
So, to summarise, it provides a datatype to encapsulate a pair of get/set functions, some infix sugar for using it, and some TH for autogenerating said data. Is that about right?
Exactly. 'modify' or (^:) is what you are looking for.

I hope Haskell prime will be a bit more "first class" when it comes to records. On Mon, Mar 30, 2009 at 11:14 PM, Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Sun, 29 Mar 2009, Andrew Coppin wrote:
Ryan Ingram wrote:
Take a look at Data.Accessor on hackage:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor-tem...
So, to summarise, it provides a datatype to encapsulate a pair of get/set functions, some infix sugar for using it, and some TH for autogenerating said data. Is that about right?
Exactly. 'modify' or (^:) is what you are looking for.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Now suppose that foo3 :: [Int], and I want to prepend 5 to it. I end up having to write something like
let v1 = v0 {foo3 = 5 : (foo3 v0)}
There used to be a feature in pre-Haskell'98 called "named field puns", which allows to use a shorter form of field access. Your example would come out as f v0{foo3} = let v1 = v0 {foo3 = 5: foo3} in ... Have a look in the Haskell-1.4 Language Report to see how it works. The behaviour is still supported as an extension by Hugs (-98), nhc98 (-puns), and ghc (-XNamedFieldPuns). Regards, Malcolm
participants (6)
-
Andrew Coppin
-
Henning Thielemann
-
Hugo Pacheco
-
Malcolm Wallace
-
Peter Verswyvelen
-
Ryan Ingram