Dave Cottingham - 2004-07-30

Logged In: YES
user_id=50465

Correct me if I'm wrong, but I believe your definition of f
is not tail-recursive, and hence (f x) will build x frames
on the stack before collapsing the whole thing. If x is
large, things can blow up.

One might then fruitfully argue whether a segfault is the
appropriate response, or if it should check for this
condition. There might be efficiency considerations.

Here's a rough draft of a tail-recursive version of f, which
requires a helper f1. This one (if I did it right) will get
the same answer but the stack space required does not depend
on x.

(define (f1 x y) (if (zero? x) y (f1 (- x 1) (+ x y))))
(define (f x) (f1 x 0))