For Programmers: Free Programming Magazines  


Home > Archive > Scheme > March 2008 > recursion problems









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author recursion problems
Janzo

2008-03-10, 8:20 am

I'm in troubles with a simple recursion

(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.

Jussi Piitulainen

2008-03-10, 8:20 am

Janzo <juanmanolo@gmail.com> writes:

> I'm in troubles with a simple recursion
>
> (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?


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 ...))).
Janzo

2008-03-10, 8:20 am

On Mar 10, 2:06 pm, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> Janzo <juanman...@gmail.com> writes:
>
>
>
> You reduce (sum 2) to (+ 2 (sum -1)).
>
> You reduce (sum -1) to (+ -1 (sum 2)).
>


I didn't see that!
(- 1 x) != (- x 1) ,of course.
Thanks a lot.


> You build a tower of pending additions (+ 2 (+ -1 (+ 2 ...))).


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com