3. Structures
3.1. Structures
Structures package several fields into one new type.
structure Point where
x : Float
y : Float
def origin : Point :=
{ x := 0.4, y := 0.4 }
#eval origin
#eval origin.x
#eval origin.y
def add_points (p1 : Point) (p2 : Point) : Point :=
{ x := p1.x + p2.x, y := p1.y + p2.y }
#eval add_points {x := 1, y := 1} {x := 3, y := 4}
3.2. Exercises
-
Define a structure
Segmentwith endpointsa b : ℝ. -
Write a function
length : Segment → ℝ. -
Define a function that adds two segments endpoint-wise.