I am not sure if this is a stupid question but I was going through the tutorial that comes with VS 2010 and there is a function like this:
let rec factorial n = if n=0 then 1 else n * factorial (n-1)
What’s the reason of this recursive function to be marked with the rec keyword?
Is it so that the compiler is assured of it being recursive so can do certain optimizations?
What happens if you exclude it?
This might be instructive:
That prints
Now if we comment out
letand uncommentlet rec, then it printsSo from that point of view, it’s just about name binding;
let recputs the identifier in scope immediately (in this example, shadowing the previousf), whereasletputs the identifier in scope only after its body is defined.The motivation for the rule does stem from interactions with type inference.