This project is read-only.
Basic Records

Category:
Defining Types
Description: Records are concrete type definitions that hold data in an unordered, named structure.
Code:
type Point = { x: float; y: float}
type Triangle = { p1: Point; p2: Point; p3: Point }
type Vector = { dx: float; dy: float}

let origin = { x = 0.0; y = 0.0 }
let onex = { x = 1.0; y = 0.0 }
let oney = { x = 0.0; y = 1.0 }
let diff p1 p2 = { dx = p2.x - p1.x; dy = p2.y - p1.y }
let sides tri =
diff tri.p2 tri.p1,
diff tri.p3 tri.p2,
diff tri.p1 tri.p3

let RecordSample1() =
let triangle1 = { p1=origin;p2=onex;p3=oney }
printfn "triangle1 = \n%A" triangle1;
printfn "sides(triangle1) = \n%A" (sides triangle1)

Execution Result:
triangle1 = 
{p1 = {x = 0.0;
y = 0.0;};
p2 = {x = 1.0;
y = 0.0;};
p3 = {x = 0.0;
y = 1.0;};}
sides(triangle1) =
({dx = -1.0;
dy = 0.0;}, {dx = 1.0;
dy = -1.0;}, {dx = 0.0;
dy = 1.0;})

Last edited Sep 14, 2011 at 3:58 AM by ttliu2000, version 1

Comments

No comments yet.