Autoplay

    Recursive Functions in Reason

    Nik GrafNik Graf

    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.

    reasonReason
    Code

    Code

    Become a Member to view code

    You must be a 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
    Transcript

    Transcript

    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.

    Discuss

    Discuss