Recursive Functions in Reason

Nik Graf
InstructorNik Graf

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 3 years ago

Function recursion requires the let binding to be accessible inside it's own local scope. Using the rec keyword in ReasonML we can enable this visibility to declare recursion functions.

Instructor: [00:00] We create a function that counts up to 10 and brings out the parameter after each increment. Our function calls itself recursively, with the parameter being incremented by one with each call. Unfortunately, we receive an error when doing so.

[00:19] The issue here is that by default, the function body doesn't let access to the let binding that the function points to. Including the rec keyword makes this possible. This allows functions to see and call themselves to provide us the power of recursion.

[00:37] Sometimes, though, we want mutual recursive functions, and this is also possible. Start with a single recursive function using the rec keyword, and then add a second one using the end keyword.

[00:57] Note that there is no semicolon ending at the first line, and no let on the second line.