Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [O'Caml-tip] This is seriously cool
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Wed Aug 11, 2004 7:11 pm   Post subject: [O'Caml-tip] This is seriously cool

Just piced this one up from comp.lang.functional.

Let's say I want to copy a structure, but want to change one member.

For starters, the structure describes a point in three-dimensions.

code:
type point3d = { x : int; y : int; z : int }


Now I define one point:

code:
let first_point = { x = 1; y = 6; z = 42 }


Now I want to copy the first point into a second point, but zero the z coordinate. The lengthy (and thus more error-prone) method is:

code:
let second_point = {x = first_point.x; y = first_point.y; z = 0 }


Instead, how about...

code:
let second_point = { first_point with z = 0 }
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: