Hi All,
I'm working on a macro using keyword args, and I wish to have a
unnamed default like this:
(defmacro test (&key arg)
(when (eq arg 't) (setq arg (gensym)))
...
The idea is I can call it any of three ways:
(test)
(test :arg my-arg)
(test :arg t)
This works fine as long as I use T in that last form and not, say,
'T. But for the sake of the fact that I will always forget and first
try 'T, I'd like to support that too.
Any ideas? (I'm using SBCL...) I suppose this ? applies to functions
as well as macros. Thanks!
|
|
0
|
|
|
|
Reply
|
daniel.eliason (172)
|
12/2/2008 7:05:10 PM |
|
On Dec 2, 1:05=A0pm, fortunatus <daniel.elia...@excite.com> wrote:
> Hi All,
>
> I'm working on a macro using keyword args, and I wish to have a
> unnamed default like this:
>
> (defmacro test (&key arg)
> =A0 (when (eq arg 't) (setq arg (gensym)))
> =A0 ...
>
> The idea is I can call it any of three ways:
>
> (test)
> (test :arg my-arg)
> (test :arg t)
>
> This works fine as long as I use T in that last form and not, say,
> 'T. =A0But for the sake of the fact that I will always forget and first
> try 'T, I'd like to support that too.
Remember that 'T =3D=3D> (QUOTE T) at read time. Also, you could just use
GENSYM as the default value for the key, which would be a lot easier:
(DEFMACRO TEST (&KEY (ARG (GENSYM)))
...)
But it's hard to give much more help beyond that without knowing what
you want the macro to do. My gut tells me you're heading down the
wrong path with whatever solution you have in mind.
HTH, ;-)
Jeff M.
|
|
0
|
|
|
|
Reply
|
massung (339)
|
12/2/2008 7:17:06 PM
|
|
> Remember that 'T =3D=3D> (QUOTE T) at read time.
Ah - yes! If I use EQUALP rather than EQ, that works - thanks!
> Also, you could just use
> GENSYM as the default value for the key, which would be a lot easier:
>
> (DEFMACRO TEST (&KEY (ARG (GENSYM)))
> =A0 ...)
That precludes one of the forms I wish to support, when the keyword
and arg are omitted, in which case I avoid generating some code...
> My gut tells me you're heading down the
> wrong path with whatever solution you have in mind.
Mine too, I agree! ;-> But it's working now, and somewhat
reasonable...
|
|
0
|
|
|
|
Reply
|
daniel.eliason (172)
|
12/2/2008 7:44:21 PM
|
|
> Ah - yes! =A0If I use EQUALP rather than EQ, that works - thanks!
Actually, I take it back - this works on CLISP where I tried it first,
but not on SBCL. Dang!
|
|
0
|
|
|
|
Reply
|
daniel.eliason (172)
|
12/2/2008 7:57:30 PM
|
|
> > Ah - yes! =A0If I use EQUALP rather than EQ, that works - thanks!
> Actually, I take it back - this works on CLISP where I tried it first,
> but not on SBCL. =A0Dang!
To support both (TEST :ARG T) and (TEST :ARG 'T) calls, on CLISP
all I had to do was:
(defmacro test (&key arg)
(when (equalp arg 't) (setq arg (gensym)))
...
Whereas on SBCL I had to do:
(defmacro test (&key arg)
(when (or (eq 't arg)
(equalp '(quote t) arg)) (setq arg (gensym)))
...
Which does NOT work the same way on CLISP... So now it's working, but
what is the closest to the Common Lisp standard behaviour???
BTW, my gut really hurts now!!! ;->
|
|
0
|
|
|
|
Reply
|
daniel.eliason (172)
|
12/2/2008 9:45:12 PM
|
|
On Dec 2, 4:45=A0pm, fortunatus <daniel.elia...@excite.com> wrote:
> > > Ah - yes! =A0If I use EQUALP rather than EQ, that works - thanks!
> > Actually, I take it back - this works on CLISP where I tried it first,
> > but not on SBCL. =A0Dang!
>
> To support both (TEST :ARG T) and (TEST :ARG 'T) calls, on CLISP
> all I had to do was:
>
> (defmacro test (&key arg)
> =A0 (when (equalp arg 't) (setq arg (gensym)))
> =A0 ...
>
> Whereas on SBCL I had to do:
>
> (defmacro test (&key arg)
> =A0 (when (or (eq 't arg)
> =A0 =A0 =A0 =A0 =A0 =A0 (equalp '(quote t) arg)) (setq arg (gensym)))
> =A0 ...
>
> Which does NOT work the same way on CLISP... =A0So now it's working, but
> what is the closest to the Common Lisp standard behaviour???
>
> BTW, my gut really hurts now!!! =A0;->
Well, T evaluates to itself. So you shouldn't quote it (there isn't
any point).
--------------
from http://sunsite.univie.ac.at/textbooks/cltl/clm/node70.html (CLTL)
[Constant]
t
The value of t is always t.
---------------
It may be that in one implementation T is its own boolean type,
Whereas in another it is actually a symbol which evaluates to itself.
|
|
0
|
|
|
|
Reply
|
anonymous.c.lisper (176)
|
12/3/2008 1:54:51 AM
|
|
fortunatus <daniel.eliason@excite.com> writes:
Well, of course, the real answer is to learn to use your macro properly
and not invoke it with 'T or 'VAR-NAME either. In fact, I bet if you
called it with
(test :arg 'my-variable)
it wouldn't do what you want either.
So pass it the proper arguments, namely unquoted symbols.
> Whereas on SBCL I had to do:
>
> (defmacro test (&key arg)
> (when (or (eq 't arg)
> (equalp '(quote t) arg)) (setq arg (gensym)))
> ...
But if you insist on ignoring my advice:
This is really the only safe way. You could also use EQUAL rather than
EQUALP, by the way. That is because you could be getting either T or
(QUOTE T) as the input. I'm guessing that CLISP may do some
short-circuiting of reading quoted constants like T, NIL and perhaps
numbers as well.
--
Thomas A. Russ, USC/Information Sciences Institute
|
|
0
|
|
|
|
Reply
|
tar (1630)
|
12/3/2008 2:46:23 AM
|
|
tar@sevak.isi.edu (Thomas A. Russ) writes:
> [...] I'm guessing that CLISP may do some
> short-circuiting of reading quoted constants like T, NIL and perhaps
> numbers as well.
Not at all. It's conformant.
--
__Pascal Bourguignon__
|
|
0
|
|
|
|
Reply
|
pjb (7667)
|
12/3/2008 12:38:29 PM
|
|
On Dec 2, 8:54=A0pm, anonymous.c.lis...@gmail.com wrote:
> Well, T evaluates to itself. So you shouldn't quote it (there isn't
> any point).
Aren't you saying that T evaluates to 'T? I mean, the expression T
(which is a variable) evaluates to the symbol T (which is a value)?
But then if I quote it, doesn't it (conceptually at least) save the
need to evalutate?
So that by quoting T, I am directly coding the value I wish to have in
the expression at that point.
|
|
0
|
|
|
|
Reply
|
daniel.eliason (172)
|
12/3/2008 2:51:35 PM
|
|
On Dec 3, 7:38=A0am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> t...@sevak.isi.edu (Thomas A. Russ) writes:
>
> > [...] I'm guessing that CLISP may do some
> > short-circuiting of reading quoted constants like T, NIL and perhaps
> > numbers as well.
>
> Not at all. =A0It's conformant.
>
> --
> __Pascal Bourguignon__
Is the SBCL behaviour also conformant?
|
|
0
|
|
|
|
Reply
|
daniel.eliason (172)
|
12/3/2008 2:54:13 PM
|
|
daniel.eliason@excite.com writes:
> On Dec 3, 7:38�am, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> t...@sevak.isi.edu (Thomas A. Russ) writes:
>>
>> > [...] I'm guessing that CLISP may do some
>> > short-circuiting of reading quoted constants like T, NIL and perhaps
>> > numbers as well.
>>
>> Not at all. �It's conformant.
>
> Is the SBCL behaviour also conformant?
Yes of course. There's no difference in behavior; when you pass 1 to
a macro, you get 1, when you pass '1 you get '1.
[pjb@simias :0.0 ~]$ clall '(defmacro m (arg) `(list (type-of (quote ,arg)) (quote ,arg)))' '(values (m t) (m (quote t)) (m 1) (m (quote 1)))'
========================================================================
CLISP 2.43 (2007-11-18) (built 3414750461) (memory 3414750593)
Evaluation of
(DEFMACRO M (ARG) `(LIST (TYPE-OF ',ARG) ',ARG))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> M
Evaluation of
(VALUES (M T) (M 'T) (M 1) (M '1))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> (BOOLEAN T) ;
(CONS 'T) ;
(BIT 1) ;
(CONS '1)
; loading system definition from
; /usr/share/common-lisp/systems/asdf-binary-locations.asd into
; #<PACKAGE "ASDF0">
; registering #<SYSTEM ASDF-BINARY-LOCATIONS {100273BDB1}> as
; ASDF-BINARY-LOCATIONS
========================================================================
SBCL 1.0.14-gentoo
Evaluation of
(DEFMACRO M (ARG) `(LIST (TYPE-OF ',ARG) ',ARG))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> M
Evaluation of
(VALUES (M T) (M 'T) (M 1) (M '1))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> (BOOLEAN T) ;
(CONS 'T) ;
(BIT 1) ;
(CONS '1)
========================================================================
GNU Common Lisp (GCL) GCL 2.6.7
Evaluation of
(DEFMACRO M (ARG)
(LIST 'LIST (LIST 'TYPE-OF (LIST 'QUOTE ARG)) (LIST 'QUOTE ARG)))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> M
Evaluation of
(VALUES (M T) (M 'T) (M 1) (M '1))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> (SYMBOL T) ;
(CONS 'T) ;
(FIXNUM 1) ;
(CONS '1)
========================================================================
ECL 0.9j
Evaluation of
(DEFMACRO M (ARG)
(SI:QUASIQUOTE (LIST (TYPE-OF '(SI:UNQUOTE ARG)) '(SI:UNQUOTE ARG))))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> M
Evaluation of
(VALUES (M T) (M 'T) (M 1) (M '1))
produced nothing on *STANDARD-OUTPUT*
produced nothing on *ERROR-OUTPUT*
produced no error
produced the following vals:
--> (BOOLEAN T) ;
(CONS 'T) ;
((INTEGER 1 1) 1) ;
(CONS '1)
========================================================================
--
__Pascal Bourguignon__
|
|
0
|
|
|
|
Reply
|
pjb (7667)
|
12/3/2008 3:35:27 PM
|
|
On Dec 3, 9:51=A0am, daniel.elia...@excite.com wrote:
> On Dec 2, 8:54=A0pm, anonymous.c.lis...@gmail.com wrote:
>
> > Well, T evaluates to itself. So you shouldn't quote it (there isn't
> > any point).
>
> Aren't you saying that T evaluates to 'T? =A0I mean, the expression T
> (which is a variable) evaluates to the symbol T (which is a value)?
>
No. 't should evaluate to t, but t does not evaluate to 't
> But then if I quote it, doesn't it (conceptually at least) save the
> need to evalutate?
Is quote evaluated? (yes)
>
> So that by quoting T, I am directly coding the value I wish to have in
> the expression at that point.
Imagine a keyword. Would you quote a keyword?
The whole point of the keyword is that you don't have to quote it.
---------------------------------------
An additional complication being that in the lambda of a macro,
things are not evaluated until the return-form.
(i.e. you can pass a symbol to a macro at top level)
ex.
>(defmacro foo (arg)
(print arg)
`(print ,arg))
>(foo bar)
BAR
>>> Error: {Determining function in error.}
>>> Error:Unbound variable: BAR
(#<COMPILED-FUNCTION 3:E196> ...)
---------------------------
so what you 'get' with your expansion is:
>(foo 't) ;what you enter
>(quote t) ;this print is in your macro expression [code to 'create' the =
form to be expanded]
>T ; this print is the macro expansion
>T ;this is what the print statement returns
---------------------------
So you see (eq t (quote t)) doesn't evaluate to t.
|
|
0
|
|
|
|
Reply
|
anonymous.c.lisper (176)
|
12/3/2008 9:02:11 PM
|
|
On 2008-12-03, Thomas A. Russ <tar@sevak.isi.edu> wrote:
> (QUOTE T) as the input. I'm guessing that CLISP may do some
> short-circuiting of reading quoted constants like T, NIL and perhaps
> numbers as well.
The expressions (QUOTE T) and T only mean the same thing when they are an
evaluated portion of code. Reducing one to the other at read time would be
unbelievably broken.
|
|
0
|
|
|
|
Reply
|
kkylheku (2499)
|
12/3/2008 9:16:10 PM
|
|
|
12 Replies
33 Views
(page loaded in 0.175 seconds)
Similiar Articles: macro & ranuni - comp.soft-sys.sas18 19 %macro regressit(bmbavalue); 20 ... de/Styles/nls/Eng/EGDefault.css") > 15 ; > NOTE: Writing HTML Body file: #LN00008 > > > QUESTION 1. User written macro or SAS supplied macro ?? - comp.soft-sys.sas ...Tips on writing sas macros - Data Savant Consulting A well-written SAS ... SASAUTOS= Question - comp.soft-sys.sas User written macro or SAS supplied macro ?? - comp.soft-sys ... please help: macro question - comp.soft-sys.sas... or not??? > Please help. ... some one help me to write a macro ... GARCH(1,1) MLE Question - comp.soft-sys.matlab Re: SAS Macro question 0 2 (6/26/2003 11:53:27 PM) Nan Bing ... Makefile: set macro once (Solaris make, GNU make) - comp.unix ...Hello, I have a question on a problem that is ... be evaluated for each usage of the macro SRC ... SRC:sh = pwd | sed 's:\(.*/src\)/.*:\1:' Does anybody know of a way to write a ... Where used macro? - comp.cad.solidworksHi, Does anyone where to find a macro which can write the assembly name in which the part ... Post Question | Groups ... Finding the nth business day of a given month and year - comp.soft ...... Post Question | Groups ... let nthday=%eval(&nthday +1); %let n=%eval(&n +1); %end; /* checks the DAY macro variable by writing it ... Macros in putty... - comp.unix.solarisIs there any way to create macros in Putty, like I ... Post Question | Groups ... What about writing a shell script at the target machine? finding the most repeated value - comp.soft-sys.matlabhow can i write a macro to find the most repeated value in matrix ... ... Post Question | Groups ... Can anyone help me with assembly language? - comp.lang.asm.x86 ...... am having trouble following assembly language can someone answer these questions? 1. Write ... Create a macro named mMult32 that multiplies two unsigned 32-bit memory ... excel macro to create insert statements - comp.databases.mysql ...are there any excel macros that will create insert ... Post Question | Groups ... How to print (write) an arraw in excel from Matlab in ... How to print (write) an arraw in excel from Matlab in colors ...To do this you have to: 1.) open connection ... Dear Michal, I have one doubt about writing data in ... Key press question - comp.lang.perl.misc A while ago I ... Parsers, grammars and BNF - comp.compilersUnlike most macro-processors which ... opt or in EBMF A?), you write a rule like: A-opt: A | /* empty */; The only real question s one faces are: 1) to ... Filling paragraph with no wrap - comp.emacsThis question will be maddeningly obvious to some, and ... Put another way, let's say I write a paragraph of text ... I'll look into how to make this a saved macro available ... How to pass dataset variables' value to macro - comp.soft-sys.sas ...... Post Question ... 33;output; run; I want to write the code in which macro ... input > > parameters should be 1,2,3 and for second macro ... Writing to a netcdf file that has a "record dimension" - comp.soft ...... Post Question | Groups ... used macro? - comp.cad.solidworks Hi, Does anyone where to find a macro which can write ... Chapter 2: The Basics of Writing and Testing VBA Code (Part 1 of 2)In general, it is a good programming practice to write ... ribbon from the toolbar, and then select Macro in the ... happen to notice that this time you had to use a question ... How do I write a Macro in Excel? - Yahoo! AnswersBest Answer: You record Macros in excel, just click record, do the action and click stop. Then add the macro to a button. I think this is what youre asking ... 7/28/2012 9:45:50 PM
|