[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: defmacro

William James

4/9/2015 12:47:00 AM

Thomas A. Russ wrote:

> > I'm writing a defmacro which expands to a do loop and calls an
> > iterator. I think I have most of it done but the do syntax is kind of
> > confusing.
>
>
> Well, maybe it would be easier to expand into LOOP code....
>
> > Any help would be appreciated. I apologize if it's not the best syntax.
>
> It can probably be simplified a bit depending on your conventions. For
> example, if the argument variable for the iteration has to be a SYMBOL
> (which is a reasonable expectation), you don't need to go through the
> trouble of creating a GENSYM for it and binding it to the unevaluated
> result. You can use it dirctly. Also, since you presumably want to
> evaluate the iterator argument, you don't want to quote it. You do want
> to use a GENSYM for that, since it would be bad to evaluate it multiple
> times.
>
> Also, you can take advantage of argument list destructuring in Macro
> lambda argument lists. DEFMACRO is more flexible in that way than
>
> > (defmacro doloop (vars &body body)
> > (let ((arg (gensym))
> > (itr (gensym)))
> > `(let ((,arg ',(car vars))
> > (,itr ',(cadr vars)))
> > (do ((,arg() (funcall (iter-n ,itr)))
> > ((,v ,arg) ((null (funcall (iter-has-n ,itr)) arg))))
> > ,@body))))
>
> Of course, one of the big problems is that the parentheses don't
> properly balance for the DO loop. From the indenting, I think you want
> the first clause to be simply the stepping code for the iteration. The
> end test is presumably the code done with the NULL.
>
> Also, it isn't at all clear that FUNCALL is needed, but since we don't
> really have any clue as to what the interface to your iterators is, it's
> bit hard to tell for sure. But I suspect you don't want to use FUNCALL
> at all.
>
> Finally, there is this ,v that refers to an unbound argument.
>
> Perhaps this untested code is closer to what you want. I'm not
> generally up on DO syntax, since I hate it and use LOOP instead, so
> there may be some bugs.
>
> (defmacro doloop ((var iterator-form) &body body)
> (let ((itr (gensym)))
> `(let ((,itr ,iterator-form))
> (do ((,var () (iter-n ,itr)))
> ((null (iter-has-n ,itr)) ,var)
> ,@body))))


Gauche Scheme:

(use gauche.generator)

(define my-gen (list->generator '(a b c)))

(until (my-gen) eof-object? => item
(print item))

===>
a
b
c
#t