[racket] recursion?? two
On Wed, Jun 06, 2012 at 03:17:30AM -0700, Ronald Reynolds wrote:
> Is it correct to say that when I call a function inside of it's own
> definition I am just making it repeat loop?
> (define (my-map f lst)
> (cond
> [(empty? lst) empty]
> [else (cons (f (first lst))
> (my-map f (rest lst)))]))
> Is this code telling racket to repeat until lst is empty? Thx to all
> for your help.
>
Yes, it does keep calling itself with smaller and smaller tails of the
original list until the list is empty.
And this part couldl be easily done in a loop.
But where this differs from a loop is that when all these functino calls
start returning. it does work on the way out -- all those cons'es. That
is more difficult to do with a loop.
-- hendrik