Lesson 11 of 15
Algebraic Types
Variant Types (Algebraic Data Types)
OCaml lets you define custom types with type:
type color = Red | Green | Blue
Each variant is a constructor. You match on them:
let color_to_string c =
match c with
| Red -> "red"
| Green -> "green"
| Blue -> "blue"
Variants with Data
Constructors can carry data:
type shape =
| Circle of float
| Rectangle of float * float
Since our transpiler erases types, we will work with simple integer-tagged variants for this lesson.
Your Task
Write a function classify that takes an integer and returns "negative", "zero", or "positive".
JS Transpiler loading...
Loading...
Click "Run" to execute your code.