You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Gren will correctly optimize tail-recursive functions to avoid the possibility of stack overflow exceptions.
countDown:Int->IntcountDown value =if value <1then 0else
countDown (value -1)
In the example above, countDown is recognized by the compiler as tail-recursive, and will be compiled into a while loop that doesn't consume stack space. However, if you were to somehow get this wrong then you'd get no warning from the compiler that your function isn't stack safe. For beginners, it's also not always apparent what is and isn't stack safe.
This proposal introduces the recur keyword, which is a hint to the compiler that the function is meant to be stack safe in the face of recursion. The compiler can then check, and potentially fail, if that doesn't turn out to be the case.
countDown:Int->IntcountDown value =if value <1then0else
recur (value -1)
The recur keyword can also work in the face of optimizations such as tail recursion modulo cons, which makes it a bit harder to understand all the potential cases where recursion is stack safe.
The text was updated successfully, but these errors were encountered:
Gren will correctly optimize tail-recursive functions to avoid the possibility of stack overflow exceptions.
In the example above,
countDown
is recognized by the compiler as tail-recursive, and will be compiled into awhile
loop that doesn't consume stack space. However, if you were to somehow get this wrong then you'd get no warning from the compiler that your function isn't stack safe. For beginners, it's also not always apparent what is and isn't stack safe.This proposal introduces the
recur
keyword, which is a hint to the compiler that the function is meant to be stack safe in the face of recursion. The compiler can then check, and potentially fail, if that doesn't turn out to be the case.The recur keyword can also work in the face of optimizations such as tail recursion modulo cons, which makes it a bit harder to understand all the potential cases where recursion is stack safe.
The text was updated successfully, but these errors were encountered: