Hi does rust do tail call optimization? The reason I am asking is that the
following tail call recursive implementation results in a stack overflow.
Thanks.
*********************************************************************************
//In this program we check (via a tail recursive function)
//whether 200003 (which is a prime number) has any divisors.
//Clearly the answer is negative, but the aim is to illustrate
//the stack overflowing.
/*
Starting with a maximum value of I-1, J is decreased
and tested to see if it is a divisor of I. If I
has a divisor, then the function returns true,
otherwise it returns false.
*/
fn divisibleRec(I: int, J:int) -> bool {
if J==1 { false }
else{
if I%J==0 { true }
else{ divisibleRec(I,J-1) }
}
}
//Checks whether I is divisible by any number below it.
fn divisible(I: int) -> bool {
divisibleRec(I, I-1)
}
fn main() {
let number = 200003;//200003 is a prime number
let result_isNumDivisible = divisible(number);
io::println(bool::to_str(result_isNumDivisible));
}
*********************************************************************************
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev