[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Please review macro

William James

3/10/2016 3:53:00 PM

> > > 1 (defmacro collect (var form &key (append-elements t))
> > > "\"Appends\" value of FORM to value of VAR and sets VAR to resulting
> > > value. If FORM is an atom, it's added to the list. If it's a list, its
> > > elements are added unless keyword APPEND-ELEMENTS is nil, where the list
> > > itself is added. The value of VAR must be of type (or LIST NIL)."
> > > 2 (let ((form@ (gensym)))
> > > 3 `(let ((,form@ ,form))
> > > 4 (setq ,var (append ,var
> > > 5 ,(if append-elements
> > > 6 `(if (listp ,form@)
> > > 7 ,form@
> > > 8 (list ,form@))
> > > 9 `(list ,form@)))))))
> > >
> > > My main question is if there's some function, or simple combination of
> > > functions, in CL that does this. (I guess it could be argued that this
> > > macro's a pretty simple combination.) And no, I don't want to use LOOP.
> > > I guess I really don't want to learn LOOP. That's probably why I wrote
> > > this macro.
> >
> > You *should* learn loop; it helps a lot, much of the time. I don't see how
> > it would help here, though... I'm just a newbie, though.
>
> Actually, I think the loop solution is sufficiently concise that it
> doesn't need any additional wrappers. Here is what it would look like:
>
> (setq var (loop for element in form
> when (and append-elements (listp element))
> append element
> else collect element))

Scheme:

(let ((append-elements #t))
(append-map
(lambda (x)
(if (and append-elements (list? x))
x
(list x)))
'(2 (3 4) () 5)))

===>
(2 3 4 5)

(let ((append-elements #f))
(append-map
(lambda (x)
(if (and append-elements (list? x))
x
(list x)))
'(2 (3 4) () 5)))

===>
(2 (3 4) () 5)


--
When the Israeli bombers and torpedo-planes were sent to attack and destroy the
ship, the Jewish commander, seeing that it was an American vessel, had
misgivings and reported to the High Command, which simply repeated the orders
to attack and sink the Liberty. www.revilo-oliver.com/rpo/Bit_of_Good_News.html