On 02/02/2012 11:47 PM, H. S. Teoh wrote:
On Thu, Feb 02, 2012 at 10:55:06PM +0100, Timon Gehr wrote:
On 02/02/2012 08:04 PM, xancorreu wrote:
[...]
For the other hand, how can increase the stack in linux?
[...]
I don't know, but it is best to just rewrite the code so that it does
not use recursion.
(This kind of problem is exactly the reason why any language standard
should mandate tail call optimization.)
Doesn't help badly-chosen implementations like:
int fib(int n) {
if (n<= 2) return 1;
else return fib(n-2) + fib(n+1);
}
This is not a tail-recursive function. And neither is recFactorial, my
bad. Anyway, my point was that the compiler should not generate code
that blows up on a (in principle) perfectly sane implementation.
There's not much the compiler can do to offset programmers choosing the
wrong algorithm for the job.
Agreed.
It can't replace educating programmers to
not implement things a certain way unless they have to.
T
Or unless they feel like it.
LList!ulong fib(){
LList!ulong r;
r=cons(st(1UL),cons(st(1UL),lz(()=>zipWith((Lazy!ulong a,
Lazy!ulong b)=>lz(()=>a+b),r,r.tail)())));
return r;
}