Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Problem with iteration

Received: by 10.68.195.131 with SMTP id ie3mr4184085pbc.8.1335949781296;
        Wed, 02 May 2012 02:09:41 -0700 (PDT)
Path: r9ni122893pbh.0!nntp.google.com!news2.google.com!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!enews4
From: "WJ" <w_a_x_...@yahoo.com>
Newsgroups: comp.lang.lisp
Subject: Re: Problem with iteration
Date: 2 May 2012 09:09:25 GMT
Organization: NewsGuy - Unlimited Usenet $19.95
Lines: 29
Message-ID: <jnqtk50nqk@enews4.newsguy.com>
NNTP-Posting-Host: p63adc90407fd6108527a283677891278b18398323041f70496c8a4910bf66bab.newsdawg.com
Mime-Version: 1.0
User-Agent: XanaNews/1.18.1.6
X-Antivirus: avast! (VPS 120501-1, 05/01/2012), Outbound message
X-Antivirus-Status: Clean
X-Received-Bytes: 1290
Content-Type: text/plain; charset=iso-8859-1

Erik Naggum wrote:

> * Raffael Cavallaro 
> | (defun my-reverse (a-list) 
> |      (do ((n (- 1) (incf n)) (result '() (cons (nth n a-list) result))) 
> |          ((= n (- (length a-list) 1))  result ))) 
> 
>   yikes. 
> 
> (defun my-reverse (list) 
>   (do ((reversed (list (pop list)) 
>                  (cons (pop list) reversed))) 
>       ((endp list) reversed)))

Buggy:

* (my-reverse '())

(NIL)


And POP won't work on immutable lists.

Let's do it the right way using Racket:

(define (my-reverse alist)
  (do ((accum '()  (cons (car alist) accum))
       (alist  alist  (cdr alist)))
      ((null? alist)   accum)))