Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Recursive Functions in Reason


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    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.