Computer Science Canada

[O'Caml-tip] This is seriously cool

Author:  wtd [ 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 }


: