scsh-users
[Top] [All Lists]

map function (tail recursive version)

To: scsh-users@scsh.net
Subject: map function (tail recursive version)
From: alexis_o_torres@yahoo.com (aTorres)
Date: Sat, 24 Apr 2004 09:46:37 +0200 (MST)
List-id: <scsh-users.list-id.scsh.net>
Old-date: 24 Apr 2004 00:45:47 -0700
Organization: http://groups.google.com
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

<Prev in Thread] Current Thread [Next in Thread>