Example code for polymorphic variant

val sqrt = Real.Math.sqrt ;
val P1 = fn M => #CPoint M {x=10.0, y = 10.0};
val P2 = fn M => #PPoint M {r=14.1421356, theta = 45.0 };
val myPointList = [P1, P2];
val distance =
 {CPoint = fn {x,y} => sqrt (x *x + y* y),
  PPoint = fn {r, theta} => r};
val angle =
 {CPoint = fn {x,y} => (Math.atan (x / y) / Math.pi) * 180.0,
  PPoint = fn {r, theta} => theta};
P1 distance ;
P2 distance ;
P1 angle ;
P2 angle ;
fun iterator L f = map (fn x => x f) L;
val myPointLstIter = fn x => iterator myPointList x;
myPointLstIter distance;
myPointLstIter angle;
Last modified:2006/02/19 08:02:32
Keyword(s):
References:[Polymorphic variant]