Search⌘ K
AI Features

Solution Review: The Factorial of a Number

Explore how to define recursive functions by implementing the factorial of a number in ReasonML. This lesson guides you through handling base cases and recursive calls using switch expressions to deepen your understanding of function recursion.

We'll cover the following...
...
Reason
let rec fact = (n: int) => {
/* Check if n is 0 or 1 */
switch (n) {
| 0 => 1; /* Base case */
| n => n * fact(n - 1) /* Make recursive calls and multiply each time */
};
};
Js.log(fact(5));
...