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))) ;
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
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,
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
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.
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!
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
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
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.