Recursive functions conversion to cycles

I’ve noticed that rescript doesn’t always convert recursive functions to cycles even in simple cases (in my opinion). For example I have:

 let rec listSum = list =>
    switch list {
    | list{} => 0.
    | list{a,} => a +. listSum(rest)


let rec listSum = (~result=0., list) =>
    switch list {
    | list{} => result
    | list{a,} => listSum(rest, ~result=result +. a)

The first one will compile to a similar function with recursion, while the second will become an optimized function with while.
I’m just curious is it because the first option isn’t considered a real recursion function in mathematical circles, or it’s just not an easy task for a compiler to track the origin of a float I plus with the function?

The optimisation is called tail call optimisation. It’ll only optimise the function if you return the recursive call directly - without doing anything else

1 Like