From: ilya
Subject: macro simplification
Date: 
Message-ID: <newscache$axkldh$jk2$1@lnews.actcom.co.il>
Hi.
I wrote this macro.
How can one simplify this macro ?

I'm not talking about using cons instead of append :))


(defmacro case-trigger (&rest clauses)
     (append '(cond)
         (mapcar
             #'(lambda (clause)
                 (apply #'list
                     `(var ,(first clause))
                     (rest clause)))
             clauses)))


Additional info:

[32]> (macroexpand-1 '(cgi::case-trigger (a (some-func1)) (b (code-2))))
(COND ((CGI::VAR A) (SOME-FUNC1)) ((CGI::VAR B) (CODE-2))) ;

From: Nils Goesche
Subject: Re: macro simplification
Date: 
Message-ID: <87r87yscrb.fsf@darkstar.cartan>
ilya <············@example.com> writes:

> I wrote this macro.
> How can one simplify this macro ?
> 
> I'm not talking about using cons instead of append :))
> 
> 
> (defmacro case-trigger (&rest clauses)
>      (append '(cond)
>          (mapcar
>              #'(lambda (clause)
>                  (apply #'list
>                      `(var ,(first clause))
>                      (rest clause)))
>              clauses)))
> 
> 
> Additional info:
> 
> [32]> (macroexpand-1 '(cgi::case-trigger (a (some-func1)) (b (code-2))))
> (COND ((CGI::VAR A) (SOME-FUNC1)) ((CGI::VAR B) (CODE-2))) ;

Maybe

(defmacro case-trigger (&body clauses)
  `(cond ,@(mapcar (lambda (clause)
                     `((var ,(car clause)) ,@(cdr clause)))
                   clauses)))

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: ilya
Subject: Re: macro simplification
Date: 
Message-ID: <newscache$7wlldh$qn2$1@lnews.actcom.co.il>
Nils Goesche wrote:
> ilya <············@example.com> writes:
> 
> 
>>I wrote this macro.
>>How can one simplify this macro ?
>>
>>I'm not talking about using cons instead of append :))
>>
>>
>>(defmacro case-trigger (&rest clauses)
>>     (append '(cond)
>>         (mapcar
>>             #'(lambda (clause)
>>                 (apply #'list
>>                     `(var ,(first clause))
>>                     (rest clause)))
>>             clauses)))
>>
>>
>>Additional info:
>>
>>[32]> (macroexpand-1 '(cgi::case-trigger (a (some-func1)) (b (code-2))))
>>(COND ((CGI::VAR A) (SOME-FUNC1)) ((CGI::VAR B) (CODE-2))) ;
> 
> 
> Maybe
> 
> (defmacro case-trigger (&body clauses)
>   `(cond ,@(mapcar (lambda (clause)
>                      `((var ,(car clause)) ,@(cdr clause)))
>                    clauses)))
> 
> Regards,

Thanks a lot. This one looks fine.
From: ilya
Subject: Re: macro simplification
Date: 
Message-ID: <newscache$rkdrdh$e2f$1@lnews.actcom.co.il>
Nils Goesche wrote:
[snip]
> 
> Maybe
> 
> (defmacro case-trigger (&body clauses)
>   `(cond ,@(mapcar (lambda (clause)
>                      `((var ,(car clause)) ,@(cdr clause)))
>                    clauses)))
> 
> Regards,

Is that ok to extend it that way to handle the special case?
(Sorry for "first" & "rest" :)

(defmacro case-trigger (&body clauses)
     `(cond
         ,@(mapcar (lambda (clause)
             (if (and (symbolp (first clause)) (eq t (first clause)))
                 `(t ,@(rest clause))
                 `((var ,(first clause)) ,@(rest clause))))
             clauses)))
From: Nils Goesche
Subject: Re: macro simplification
Date: 
Message-ID: <ly7k9lfft1.fsf@cartan.de>
ilya <············@example.com> writes:

> Nils Goesche wrote:
> [snip]
> > Maybe
> > (defmacro case-trigger (&body clauses)
> >   `(cond ,@(mapcar (lambda (clause)
> >                      `((var ,(car clause)) ,@(cdr clause)))
> >                    clauses)))
> > Regards,
> 
> Is that ok to extend it that way to handle the special case?
> (Sorry for "first" & "rest" :)
> 
> (defmacro case-trigger (&body clauses)
>      `(cond
>          ,@(mapcar (lambda (clause)
>              (if (and (symbolp (first clause)) (eq t (first clause)))
>                  `(t ,@(rest clause))
>                  `((var ,(first clause)) ,@(rest clause))))
>              clauses)))

Yes, but note that the call to SYMBOLP is redundant:

  (if (eq t (first clause))
      ...)

is enough.  The only thing that is EQ to T is T, and T /is/ a symbol.

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: ilya
Subject: Re: macro simplification
Date: 
Message-ID: <newscache$fk1tdh$yah$1@lnews.actcom.co.il>
Nils Goesche wrote:
[snip]
>>(defmacro case-trigger (&body clauses)
>>     `(cond
>>         ,@(mapcar (lambda (clause)
>>             (if (and (symbolp (first clause)) (eq t (first clause)))
>>                 `(t ,@(rest clause))
>>                 `((var ,(first clause)) ,@(rest clause))))
>>             clauses)))
> 
> 
> Yes, but note that the call to SYMBOLP is redundant:
> 
>   (if (eq t (first clause))
>       ...)
> 
> is enough.  The only thing that is EQ to T is T, and T /is/ a symbol.

Wouldn't that {omitting (symbolp ...)} cause evaluation of
(first clause) at macro-expansion time ?

> 
> Regards,
From: Nils Goesche
Subject: Re: macro simplification
Date: 
Message-ID: <ly3ck9fa6u.fsf@cartan.de>
ilya <············@example.com> writes:

> Nils Goesche wrote:
> [snip]
> >>(defmacro case-trigger (&body clauses)
> >>     `(cond
> >>         ,@(mapcar (lambda (clause)
> >>             (if (and (symbolp (first clause)) (eq t (first clause)))
> >>                 `(t ,@(rest clause))
> >>                 `((var ,(first clause)) ,@(rest clause))))
> >>             clauses)))
> > Yes, but note that the call to SYMBOLP is redundant:
> >   (if (eq t (first clause))
> >       ...)
> > is enough.  The only thing that is EQ to T is T, and T /is/ a symbol.
> 
> Wouldn't that {omitting (symbolp ...)} cause evaluation of
> (first clause) at macro-expansion time ?

(first clause) is evaluated at macro-expansion time in any case, so I
am not sure I understand your question :-)

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: ilya
Subject: Re: macro simplification
Date: 
Message-ID: <newscache$3y2tdh$ybh$1@lnews.actcom.co.il>
Nils Goesche wrote:
> ilya <············@example.com> writes:
> 
> 
>>Nils Goesche wrote:
>>[snip]
>>
>>>>(defmacro case-trigger (&body clauses)
>>>>    `(cond
>>>>        ,@(mapcar (lambda (clause)
>>>>            (if (and (symbolp (first clause)) (eq t (first clause)))
>>>>                `(t ,@(rest clause))
>>>>                `((var ,(first clause)) ,@(rest clause))))
>>>>            clauses)))
>>>
>>>Yes, but note that the call to SYMBOLP is redundant:
>>>  (if (eq t (first clause))
>>>      ...)
>>>is enough.  The only thing that is EQ to T is T, and T /is/ a symbol.
>>
>>Wouldn't that {omitting (symbolp ...)} cause evaluation of
>>(first clause) at macro-expansion time ?
> 
> 
> (first clause) is evaluated at macro-expansion time in any case, so I
> am not sure I understand your question :-)
> 
> Regards,

That's the point I missed. Thanks.
From: Michael Livshin
Subject: Re: macro simplification
Date: 
Message-ID: <s3n0im1nqd.fsf@laredo.verisity.com.cmm>
ilya <············@example.com> writes:

> How can one simplify this macro ?
>
> I'm not talking about using cons instead of append :))
>
> (defmacro case-trigger (&rest clauses)
>      (append '(cond)
>          (mapcar
>              #'(lambda (clause)
>                  (apply #'list
>                      `(var ,(first clause))
>                      (rest clause)))
>              clauses)))

(defmacro case-trigger (&rest clauses)
  (cons 'cond                               ; sorry about that...
        (loop :for i :in clauses
           :collect `((var ,(car clause)) ,@(cdr clause)))))

[ untested ]

-- 
I am not a Church numeral!
I am a free variable!
From: Nils Goesche
Subject: Re: macro simplification
Date: 
Message-ID: <87n0imsce0.fsf@darkstar.cartan>
Michael Livshin <······@cmm.kakpryg.net> writes:

> (defmacro case-trigger (&rest clauses)
>   (cons 'cond                               ; sorry about that...
>         (loop :for i :in clauses
>            :collect `((var ,(car clause)) ,@(cdr clause)))))
> 
> [ untested ]

(obviously)

:-)
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Michael Livshin
Subject: Re: macro simplification
Date: 
Message-ID: <s3ista1mtf.fsf@laredo.verisity.com.cmm>
Nils Goesche <···@cartan.de> writes:

> Michael Livshin <······@cmm.kakpryg.net> writes:
>
>> (defmacro case-trigger (&rest clauses)
>>   (cons 'cond                               ; sorry about that...
>>         (loop :for i :in clauses
>>            :collect `((var ,(car clause)) ,@(cdr clause)))))
>> 
>> [ untested ]
>
> (obviously)

those highly mnemonic variable names always get me.  but it *does*
look nicer than mapcar, doesn't it. :)

-- 
In an experiment to determine the precise amount of beer required to
enjoy this film, I passed out.     -- dave o'brien, on Highlander II
From: Nils Goesche
Subject: Re: macro simplification
Date: 
Message-ID: <878yu6sba3.fsf@darkstar.cartan>
Michael Livshin <······@cmm.kakpryg.net> writes:

> Nils Goesche <···@cartan.de> writes:
> 
> > Michael Livshin <······@cmm.kakpryg.net> writes:
> >
> >> (defmacro case-trigger (&rest clauses)
> >>   (cons 'cond                               ; sorry about that...
> >>         (loop :for i :in clauses
> >>            :collect `((var ,(car clause)) ,@(cdr clause)))))
> >> 
> >> [ untested ]
> >
> > (obviously)
> 
> those highly mnemonic variable names always get me.  but it
> *does* look nicer than mapcar, doesn't it. :)

I tend to use MAP-functions in macros, to create some balance to
all the LOOPs in my other code.  And I think it would look even
nicer if you omitted the colons from the LOOP keywords, but
that's just me :-)

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Michael Livshin
Subject: Re: macro simplification
Date: 
Message-ID: <s3el3y1lrt.fsf@laredo.verisity.com.cmm>
Nils Goesche <···@cartan.de> writes:

> Michael Livshin <······@cmm.kakpryg.net> writes:
>
>> Nils Goesche <···@cartan.de> writes:
>> 
>> > Michael Livshin <······@cmm.kakpryg.net> writes:
>> >
>> >> (defmacro case-trigger (&rest clauses)
>> >>   (cons 'cond                               ; sorry about that...
>> >>         (loop :for i :in clauses
>> >>            :collect `((var ,(car clause)) ,@(cdr clause)))))
>> >> 
>> >> [ untested ]
>> >
>> > (obviously)
>> 
>> those highly mnemonic variable names always get me.  but it
>> *does* look nicer than mapcar, doesn't it. :)
>
> I tend to use MAP-functions in macros, to create some balance to
> all the LOOPs in my other code.

some famous russian writer (Chekhov, I think) said that all his life
he was squeezing the slave out of himself, drop by drop.  well,
apparently you don't need to squeeze the schemer out of you.  lucky
you. :)

> And I think it would look even nicer if you omitted the colons from
> the LOOP keywords, but that's just me :-)

I find that using keywords as the designators for the loop magic words
make loops look less, er, "freeform".  I'm comfortable with keywords,
but less comfortable with parsing.  I know it's still parsing, yes. :)

(loop :for i :being :all :values :of ...) does look gross, though.
when the schemer in me looks at that, he wants to forswear using loop
altogether.  *must* *not* *listen* *to* *him*.

-- 
You can sing to my cat if you like.
From: ilya
Subject: Re: macro simplification
Date: 
Message-ID: <newscache$a1uldh$9w2$1@lnews.actcom.co.il>
Nils Goesche wrote:
> Michael Livshin <······@cmm.kakpryg.net> writes:
> 
> 
>>(defmacro case-trigger (&rest clauses)
>>  (cons 'cond                               ; sorry about that...
>>        (loop :for i :in clauses
>>           :collect `((var ,(car clause)) ,@(cdr clause)))))
>>
>>[ untested ]
> 
> 
> (obviously)
> 
> :-)
Appreciate showing different approach.
It's just "s/clause/i/g" right ?
Hmm... anyway I prefer map functions because of the logic.