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.
For example, the recursive map is:
(define (map1 f l)
(cond ((NULL? l) '())
(else (cons (f (car l)) (map1 f (cdr l))))))
=> (map1 square ?(1 2 3 4 5 ))
=> (1 4 9 16 25)
Which is correct! But if I define a tail recursive map I get:
(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.
Can anyone see my error? Any suggestions?
- Alexis
|