WJ
unread,May 22, 2012, 11:22:58 AM5/22/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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