scsh-users
[Top] [All Lists]

Re: map function (tail recursive version)

To: scsh-users@scsh.net
Subject: Re: map function (tail recursive version)
From: alexis_o_torres@yahoo.com (aTorres)
Date: Sun, 25 Apr 2004 05:59:55 +0200 (MST)
List-id: <scsh-users.list-id.scsh.net>
Old-date: 24 Apr 2004 20:59:12 -0700
Organization: http://groups.google.com
"Andrew Koenig" <ark@acm.org> wrote in message 
news:<5cvic.17452$_o3.557832@bgtnsc05-news.ops.worldnet.att.net>...
> You pretty much need to build up the result backwards, then reverse it.

Thanks a lot for your advice!

Here is the final code:

; map function, tail recursive versiion
(define (map func lst) 
  (if (NULL? lst) '() (iter-map '() func lst)))

; iter-map , auxiliary function used by map
(define (iter-map curr func lst)
     (cond ((NULL? lst) (reverse curr))
           (else (iter-map (cons (func (car lst)) curr) func (cdr lst)))))

; reverse function, tail recursive versiion
(define (reverse lst)
  (if (NULL? lst) '() (iter-reverse '() lst)))

; iter-reverse , auxiliary function used by reverse
(define (iter-reverse curr lst)
  (cond ((NULL? lst) curr)
        (else (iter-reverse (cons (car lst) curr) (cdr lst)))))

Let me know of any recommendations.
- Alexis

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