Lesson 13 of 15

Records

Records

In OCaml, records group named fields into a single value:

type person = { name : string; age : int }
let alice = { name = "Alice"; age = 30 }

You access fields with dot notation: alice.name.

Simulating Records

Since our transpiler uses a simplified subset, we will represent records as tuples and use accessor functions — a common functional pattern:

(* person = (name, age) *)
let make_person name age = (name, age)
let person_name p = fst p
let person_age p = snd p

This is how many functional programs work internally — records are syntactic sugar over tuples with named accessors.

Your Task

Create a "point" abstraction using tuples: make_point, point_x, point_y, and a manhattan function that computes the Manhattan distance between two points (|x1-x2| + |y1-y2|).

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