email@example.com (aTorres) wrote in message
> I am new to scheme and am trying to implement useful recursive
> functions in to a tail recursive one. So far everything was ok, but
> now I am having trouble with making map a tail-recursive map.
> (define (map1 f l)
> (if (NULL? l) '() (iter-map1 (list (f (car l))) f (cdr l))))
> (define (iter-map1 curr f l)
> (cond ((NULL? l) curr )
> (else (iter-map1 (cons curr (f (car l))) f (cdr l)))))
> => (map square ?(1 2 3 4 5))
> => (((((1) . 4) . 9) . 16) . 25)
> Which have the correct elements in doted pairs.
(BTW, could your variables have any LESS telling names?)
The idiomatic way of doing this in Common Lisp (I can't believe the
Scheme way is that different), is to push (ie (cons data acc)) data on
the accumulator (curr) and to return the accumulator, /reversed/
(nreverse, but i don't suppose that's included in R5RS). I guess you
could also use append if you didn't mind the very bad performance.