(define (sum x)
(if (= x 0)
0
(+ x (sum (- 1 x)))))
for the numbers 0 and 1 it works, but if I do (sum 2) the program
keeps thinking... and it doesn't do nothing. I'm using drscheme with
PLT, do you think it's problem of drscheme?
thanks.
You reduce (sum 2) to (+ 2 (sum -1)).
You reduce (sum -1) to (+ -1 (sum 2)).
You build a tower of pending additions (+ 2 (+ -1 (+ 2 ...))).
I didn't see that!
(- 1 x) != (- x 1) ,of course.
Thanks a lot.