Lesson 14 of 15

Records

Records

Records are named, immutable data structures with labeled fields:

type Point = { X: int; Y: int }

let p = { X = 3; Y = 4 }
printfn $"({p.X}, {p.Y})"

Create a modified copy with with:

let p2 = { p with X = 10 }
// { X = 10; Y = 4 }

Records work well as function parameters:

let distance p = sqrt (float (p.X * p.X + p.Y * p.Y))

Your Task

Define a record type Rectangle with fields Width: int and Height: int. Then write a function area that returns Width * Height, and a function perimeter that returns 2 * (Width + Height).

JS Transpiler loading...
Loading...
Click "Run" to execute your code.