Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #273: The cord jumped over and hit the power switch.


comp / comp.lang.lisp / Re: Problem with iteration

SubjectAuthor
o Re: Problem with iterationB. Pym

1
Subject: Re: Problem with iteration
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 5 Jul 2024 22:48 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Problem with iteration
Date: Fri, 5 Jul 2024 22:48:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <v69t8o$3fjgh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 06 Jul 2024 00:49:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="36dabbcb546c8dd3a3c5c81a41ee2e18";
logging-data="3657233"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yr3rMhF4A8hgL8KBUtjRI"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:9VCpJWaMeZh11/GpKix4OtamvI4=
View all headers

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)

Gauche Scheme:

(define (my-reverse alist)
(fold cons '() alist))

(define (my-reverse lst :optional (accum '()))
(if (null? lst)
accum
(my-reverse (cdr lst) (cons (car lst) accum))))

1

rocksolid light 0.9.8
clearnet tor