caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] "changing" immutable record elements
@ 2004-05-14 15:50 David Fox
  2004-05-14 16:20 ` Martin Jambon
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: David Fox @ 2004-05-14 15:50 UTC (permalink / raw)
  To: Ocaml

Unless I am missing some core language feature, it seems to me that the 
most awkward part of the language is constructing an expression that 
represents a record with a single field modified:

let set_field3 rec value =
   match rec with
       {field1=field1; field2=field2; field3=field3; field4=field4; 
field5=field5} ->
          {field1=field1; field2=field2; field3=value; field4=field4; 
field5=field5}

is there any prettier way of doing this?


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] "changing" immutable record elements
  2004-05-14 15:50 [Caml-list] "changing" immutable record elements David Fox
@ 2004-05-14 16:20 ` Martin Jambon
  2004-05-14 16:22 ` Virgile Prevosto
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Martin Jambon @ 2004-05-14 16:20 UTC (permalink / raw)
  To: David Fox; +Cc: Ocaml

On Fri, 14 May 2004, David Fox wrote:

> Unless I am missing some core language feature, it seems to me that the
> most awkward part of the language is constructing an expression that
> represents a record with a single field modified:
>
> let set_field3 rec value =
>    match rec with
>        {field1=field1; field2=field2; field3=field3; field4=field4;
> field5=field5} ->
>           {field1=field1; field2=field2; field3=value; field4=field4;
> field5=field5}
>
> is there any prettier way of doing this?

Yes!

let new_record = { old_record with field3 = value }

NB: you must specify at least one field:
let copy r = { r with some_field = r.some_field }


Martin


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] "changing" immutable record elements
  2004-05-14 15:50 [Caml-list] "changing" immutable record elements David Fox
  2004-05-14 16:20 ` Martin Jambon
@ 2004-05-14 16:22 ` Virgile Prevosto
  2004-05-14 17:02   ` Matt Gushee
  2004-05-14 16:28 ` Kenneth Knowles
  2004-05-14 16:30 ` Andrew Lenharth
  3 siblings, 1 reply; 7+ messages in thread
From: Virgile Prevosto @ 2004-05-14 16:22 UTC (permalink / raw)
  To: David Fox; +Cc: Ocaml

Le vendredi 14 mai, à  8h50 -0700,
David Fox a écrit:

> Unless I am missing some core language feature, it seems to me that
> the most awkward part of the language is constructing an expression
> that represents a record with a single field modified:
> 
> let set_field3 rec value =
>    match rec with
>        {field1=field1; field2=field2; field3=field3; field4=field4; 
> field5=field5} ->
>           {field1=field1; field2=field2; field3=value; field4=field4; 
> field5=field5}
> 
> is there any prettier way of doing this?
Yes,
let set_field_3 my_rec v = {my_rec with field3=v} 
(cf http://pauillac.inria.fr/ocaml/htmlman/manual015.html, section
6.7.3)

By the way, rec and value are keywords in ocaml. you might run
into trouble if you try to use them as identifiers ;-)


-- 
E tutto per oggi, a la prossima volta
Virgile

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] "changing" immutable record elements
  2004-05-14 15:50 [Caml-list] "changing" immutable record elements David Fox
  2004-05-14 16:20 ` Martin Jambon
  2004-05-14 16:22 ` Virgile Prevosto
@ 2004-05-14 16:28 ` Kenneth Knowles
  2004-05-14 16:30 ` Andrew Lenharth
  3 siblings, 0 replies; 7+ messages in thread
From: Kenneth Knowles @ 2004-05-14 16:28 UTC (permalink / raw)
  To: David Fox; +Cc: Ocaml

On Fri, May 14, 2004 at 08:50:32AM -0700, David Fox wrote:
> Unless I am missing some core language feature, it seems to me that the 
> most awkward part of the language is constructing an expression that 
> represents a record with a single field modified:
> 
> let set_field3 rec value =
>   match rec with
>       {field1=field1; field2=field2; field3=field3; field4=field4; 
> field5=field5} ->
>          {field1=field1; field2=field2; field3=value; field4=field4; 
> field5=field5}
> 
> is there any prettier way of doing this?

Yes

let set_field3 rec value = {rec with field3 = value}

Kenn

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] "changing" immutable record elements
  2004-05-14 15:50 [Caml-list] "changing" immutable record elements David Fox
                   ` (2 preceding siblings ...)
  2004-05-14 16:28 ` Kenneth Knowles
@ 2004-05-14 16:30 ` Andrew Lenharth
  3 siblings, 0 replies; 7+ messages in thread
From: Andrew Lenharth @ 2004-05-14 16:30 UTC (permalink / raw)
  To: David Fox; +Cc: Ocaml

let set_field3 r value = {r with field3=falue}

Have fun

Andrew Lenharth

On Fri, May 14, 2004 at 08:50:32AM -0700, David Fox wrote:
> Unless I am missing some core language feature, it seems to me that the 
> most awkward part of the language is constructing an expression that 
> represents a record with a single field modified:
> 
> let set_field3 rec value =
>   match rec with
>       {field1=field1; field2=field2; field3=field3; field4=field4; 
> field5=field5} ->
>          {field1=field1; field2=field2; field3=value; field4=field4; 
> field5=field5}
> 
> is there any prettier way of doing this?
> 
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: 
> http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: 
> http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

-- 
"It will work in practice, yes. But will it work in theory?"
--- a french diplomat's comment, recalled by Madeleine  Albright

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] "changing" immutable record elements
  2004-05-14 16:22 ` Virgile Prevosto
@ 2004-05-14 17:02   ` Matt Gushee
  2004-05-14 17:18     ` Virgile Prevosto
  0 siblings, 1 reply; 7+ messages in thread
From: Matt Gushee @ 2004-05-14 17:02 UTC (permalink / raw)
  To: caml-list

On Fri, May 14, 2004 at 06:22:01PM +0200, Virgile Prevosto wrote:
> 
> By the way, rec and value are keywords in ocaml. you might run
> into trouble if you try to use them as identifiers ;-)

No, 'value' is safe to use. The one you need to avoid is 'val'.

-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] "changing" immutable record elements
  2004-05-14 17:02   ` Matt Gushee
@ 2004-05-14 17:18     ` Virgile Prevosto
  0 siblings, 0 replies; 7+ messages in thread
From: Virgile Prevosto @ 2004-05-14 17:18 UTC (permalink / raw)
  To: caml-list

Le vendredi 14 mai, à 11h02 -0600,
Matt Gushee a écrit:

> On Fri, May 14, 2004 at 06:22:01PM +0200, Virgile Prevosto wrote:
> > 
> > By the way, rec and value are keywords in ocaml. you might run
> > into trouble if you try to use them as identifiers ;-)
> 
> No, 'value' is safe to use. The one you need to avoid is 'val'.
> 
Oops, sorry about that. I guess I've played too much with camlp4 and its
revised syntax lately...

-- 
E tutto per oggi, a la prossima volta
Virgile

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-05-14 17:18 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-14 15:50 [Caml-list] "changing" immutable record elements David Fox
2004-05-14 16:20 ` Martin Jambon
2004-05-14 16:22 ` Virgile Prevosto
2004-05-14 17:02   ` Matt Gushee
2004-05-14 17:18     ` Virgile Prevosto
2004-05-14 16:28 ` Kenneth Knowles
2004-05-14 16:30 ` Andrew Lenharth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).