Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

re: DOLIST as DO

22 views
Skip to first unread message

WJ

unread,
May 22, 2012, 11:22:58 AM5/22/12
to
Jerry Jackson wrote:

> In article <3...@soi.UUCP> a...@soi.UUCP (Alex Zatsman) writes:
>
> Path: esosun!seismo!uunet!yale!husc6!soi!alex
> From: a...@soi.UUCP (Alex Zatsman)
> Newsgroups: comp.lang.lisp
> Keywords: dolist do do*
> Date: 14 Jul 88 15:09:55 GMT
> Organization: Software Options Inc., Cambridge, Mass.
> Lines: 2
>
> Anybody knows an elegant way of representing DOLIST construct
> through DO ? How about DO* ?
>
> How about...
>
> (defmacro dolist ((var list &optional result-form) &rest body)
> (let ((listvar (gensym)))
> `(do* ((,listvar ,list (cdr ,listvar))
> (,var (car ,list) (car ,listvar)))
> ((null ,listvar) ,result-form)
> ,@body)))
>
> or...
>
> (defmacro dolist ((var list &optional result-form) &rest body)
> (let ((listvar (gensym)))
> `(do ((,listvar (cdr ,list) (cdr ,listvar))
> (,var (car ,list) (car ,listvar)))
> ()
> ,@body
> (when (null ,listvar)
> (return ,result-form)))))


Barry Margolin wrote:

> In article <2...@esosun.UUCP> jack...@esosun.UUCP (Jerry Jackson) writes:
> >(defmacro dolist ((var list &optional result-form) &rest body)
> > (let ((listvar (gensym)))
> > `(do* ((,listvar ,list (cdr ,listvar))
> > (,var (car ,list) (car ,listvar)))
> > ((null ,listvar) ,result-form)
> > ,@body)))
>
> Both versions of your macro have a bug: they have ",list" in two
> places in the expansion. This will cause the list expression to be
> evaluated twice, which could cause problems if it has side effects.
> In the above case, both the second line of the DO* form should be
>
> (,var (car ,listvar) (car ,listvar))
>
> Fixing the DO version is slightly more complicated.


Racket:

(define-syntax-rule (dolist (var list result-form ...) expr ...)
(do ((listvar list (cdr listvar))
(var null))
((null? listvar) result-form ...)
(set! var (car listvar))
expr ...))


* (dolist (x '(a b c) 'ok) (display '--) (displayln x))
--a
--b
--c
'ok

Marco Antoniotti

unread,
May 23, 2012, 6:28:01 AM5/23/12
to
On Tuesday, May 22, 2012 5:22:58 PM UTC+2, WJ wrote:
> Jerry Jackson wrote:
>
> > In article <3...@soi.UUCP> a...@soi.UUCP (Alex Zatsman) writes:
> >
> > Path: esosun!seismo!uunet!yale!husc6!soi!alex
> > From: a...@soi.UUCP (Alex Zatsman)
> > Newsgroups: comp.lang.lisp
> > Keywords: dolist do do*
> > Date: 14 Jul 88 15:09:55 GMT

A message from 1988 (that is almost 24 year ago!) from a UUCP address. I am duly impressed. The WJ AI trollbot is close to passing the Turning test.

Cheers
--
MA
0 new messages