I have defined the following function that computes the greatest common divisor of two numbers.
let rec gcd n m = if m = 0 then n else gcd m (n mod m);;
It wouldn't work without the keyword rec
.
I'm curious, why is there a need to have rec
?
There is no other function called gcd
in scope so it's clear what the compiler has to call.
Even if there would be another function gcd
in scope, the compiler could try matching in some order and see what fits, like in Haskell.
Can you give an example of why this is useful?