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: 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.
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.
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.
Note that there is no semicolon ending at the first line, and no let on the second line.
You must be a Pro Member to view code
Access all courses and lessons, track your progress, gain confidence and expertise.