Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tacking changes to a variable?

3 views
Skip to first unread message

Thaddeus L Olczyk

unread,
Feb 19, 2002, 4:07:11 PM2/19/02
to
I have a variable thads-variable.
I would like to print something to the screen
when it's value changes, or maybe even execute a break.
Can anyone make suggestions?

Hannah Schroeter

unread,
Feb 19, 2002, 4:16:03 PM2/19/02
to
Hello!

In article <3c72be34....@nntp.interaccess.com>,

You could modify the variable only through a special setf-function.
Or encapsulate the variable into a CLOS object with a single
slot and define some :after or :before method on the setter GF.

Kind regards,

Hannah.

Erik Naggum

unread,
Feb 19, 2002, 5:36:07 PM2/19/02
to
* Thaddeus L Olczyk

Use symbol macros.

///
--
In a fight against something, the fight has value, victory has none.
In a fight for something, the fight is a loss, victory merely relief.

Rolf Wester

unread,
Feb 20, 2002, 4:18:13 AM2/20/02
to

Erik Naggum wrote:

> * Thaddeus L Olczyk
> | I have a variable thads-variable.
> | I would like to print something to the screen
> | when it's value changes, or maybe even execute a break.
> | Can anyone make suggestions?
>
> Use symbol macros.
>

Could you please explain what symbol macros are or give me a hint where
I can find an explanation/example?

Thanks in anticipation.

Rolf Wester


Rahul Jain

unread,
Feb 20, 2002, 4:39:16 AM2/20/02
to
Rolf Wester <wes...@ilt.fhg.de> writes:

> Could you please explain what symbol macros are or give me a hint where
> I can find an explanation/example?

They are ways of associating a code snippet with a symbol (either
globally or in a specific lexical context) so that every appearance of
that symbol is replaced with that code snippet.

They are documented in the HyperSpec, of course.

--
-> -/- - Rahul Jain - -\- <-
-> -\- http://linux.rice.edu/~rahul -=- mailto:rj...@techie.com -/- <-
-> -/- "I never could get the hang of Thursdays." - HHGTTG by DNA -\- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
Version 11.423.999.221020101.23.50110101.042
(c)1996-2002, All rights reserved. Disclaimer available upon request.

Tim Bradshaw

unread,
Feb 20, 2002, 5:26:37 AM2/20/02
to
* Rahul Jain wrote:

> They are ways of associating a code snippet with a symbol (either
> globally or in a specific lexical context) so that every appearance of
> that symbol is replaced with that code snippet.

However there's a fairly important caveat: binding doesn't `work' - if
you have a symbol macro and then bind the resulting `variable' then
you just end up with a conventional binding, not something magic.

--tim

Vladimir Zolotykh

unread,
Feb 20, 2002, 6:01:39 AM2/20/02
to
Rahul Jain wrote:
>
> They are ways of associating a code snippet with a symbol (either
> globally or in a specific lexical context) so that every appearance of
> that symbol is replaced with that code snippet.

As I understand the subject you're speaking about SYMBOL-MACROLET.
How it could be used 'globally' ? The CLHS says 'symbol-macrolet lexically
establishes expansion functions' and 'symbol-macrolet signals an error
if a special declaration names one of the symbols being defined by
symbol-macrolet'.

Suppose the original poster meant tracking the global variable. In that
case I only could think about using SYMBOL-MACROLET every time when access
or modification of thads-variable. Probably you've meant something different ?

--
Vladimir Zolotykh

Kent M Pitman

unread,
Feb 20, 2002, 6:27:49 AM2/20/02
to
Rahul Jain <rj...@sid-1129.sid.rice.edu> writes:

> Rolf Wester <wes...@ilt.fhg.de> writes:
>
> > Could you please explain what symbol macros are or give me a hint where
> > I can find an explanation/example?
>
> They are ways of associating a code snippet with a symbol (either
> globally or in a specific lexical context) so that every appearance of
> that symbol is replaced with that code snippet.
>
> They are documented in the HyperSpec, of course.

In the Macsyma programming language, we had these kinds of operators:

prefix sin x
infix x mod y
postfix x !
matchfix [ x, y, z ]
nofix room_temperature

The difference between a "nofix function" and a "variable" was that it
potentially ran code every time, so (for example)
room_temperature + room_temperature
was not obliged to return an even number and certainly could not be
contracted to 2*room_temperature.

Technically, CL will accomodate this style, though I some lightweight macros
that think symbols are unchanging in value may break if you do this.

Most stylistically "Good" uses of symbol macros, IMO, are those which
support the notion of a variable but just store that variable in some other
place or virtualize the variable. For example,

(symbol-macrolet ((area (* width length)))
(dotimes (width 3)
(dotimes (length 3)
(print `(,length * ,width = ,area)))))

(0 * 0 = 0)
(1 * 0 = 0)
(2 * 0 = 0)
(0 * 1 = 0)
(1 * 1 = 1)
(2 * 1 = 2)
(0 * 2 = 0)
(1 * 2 = 2)
(2 * 2 = 4)
=> NIL

In principle you can do

(symbol-macrolet ((random100 (random 100)))
(dotimes (i 5) (print random100)))

37
36
43
56
87
=> NIL

or even weird stuff like:

(flet ((foo ()
(let ((r (random 100)))
(when (evenp r) (print 'foo))
r)))
(symbol-macrolet ((foo (foo)))
(dotimes (i 5) (print foo))))

FOO
0
95
FOO
30
79
FOO
4
=> NIL

But I recommend not going overboard with "overt" side-effects.

On the other hand, I do think that using this to log uses of a variable
is overt. So the following is just fine with me stylistically...

(defmacro counting-let (bindings &body forms)
(let* ((vars (mapcar #'car bindings))
(vals (mapcar #'cadr bindings))
(stores
(mapcar #'(lambda (var) (gensym (format nil "~A-STORE" var)))
vars))
(counters
(mapcar #'(lambda (var) (gensym (format nil "~A-COUNTER" var)))
vars)))
`(let ,(mapcan #'(lambda (counter store val)
(list `(,counter 0) `(,store ,val)))
counters stores vals)
(flet ,(mapcan #'(lambda (store counter)
(list `(,store () (incf ,counter) ,store)
`((setf ,store) (val) (setq ,store val))))
stores counters)
(multiple-value-prog1
(symbol-macrolet ,(mapcar #'(lambda (var store)
`(,var (,store)))
vars stores)
,@forms)
,@(mapcar #'(lambda (counter var)
`(format t "~&~D use~:P of ~S."
,counter ',var))
counters vars))))))

(counting-let ((x 3) (y 2))
(dotimes (i 4) (setq y (+ (* x x) y)) )
(+ y y))
8 uses of X.
6 uses of Y.
=> 76


(pprint (macroexpand-1
'(counting-let ((x 3) (y 2))
(dotimes (i 4) (setq y (+ (* x x) y)) )
(+ y y))))

(LET ((#:X-COUNTER24085 0)
(#:X-STORE24083 3)
(#:Y-COUNTER24086 0)
(#:Y-STORE24084 2))
(FLET ((#:X-STORE24083 () (INCF #:X-COUNTER24085) #:X-STORE24083)
((SETF #:X-STORE24083) (VAL) (SETQ #:X-STORE24083 VAL))
(#:Y-STORE24084 () (INCF #:Y-COUNTER24086) #:Y-STORE24084)
((SETF #:Y-STORE24084) (VAL) (SETQ #:Y-STORE24084 VAL)))
(MULTIPLE-VALUE-PROG1 (SYMBOL-MACROLET ((X (#:X-STORE24083))
(Y (#:Y-STORE24084)))
(DOTIMES (I 4) (SETQ Y (+ (* X X) Y)))
(+ Y Y))
(FORMAT T "~&~D use~:P of ~S." #:X-COUNTER24085 'X)
(FORMAT T "~&~D use~:P of ~S." #:Y-COUNTER24086 'Y))))

Michael Hudson

unread,
Feb 20, 2002, 6:22:11 AM2/20/02
to
Vladimir Zolotykh <gsm...@eurocom.od.ua> writes:

> Rahul Jain wrote:
> >
> > They are ways of associating a code snippet with a symbol (either
> > globally or in a specific lexical context) so that every appearance of
> > that symbol is replaced with that code snippet.
>
> As I understand the subject you're speaking about SYMBOL-MACROLET.
> How it could be used 'globally' ? The CLHS says 'symbol-macrolet lexically
> establishes expansion functions' and 'symbol-macrolet signals an error
> if a special declaration names one of the symbols being defined by
> symbol-macrolet'.

There's define-symbol-macro, too. Don't think CMUCL supports it (but
could be wrong).

Cheers,
M.

--
Programming languages should be designed not by piling feature on
top of feature, but by removing the weaknesses and restrictions
that make the additional features appear necessary.
-- Revised(5) Report on the Algorithmic Language Scheme

Kent M Pitman

unread,
Feb 20, 2002, 6:41:14 AM2/20/02
to
Vladimir Zolotykh <gsm...@eurocom.od.ua> writes:

DEFINE-SYMBOL-MACRO does the global thing.

Pierre R. Mai

unread,
Feb 20, 2002, 10:17:19 AM2/20/02
to
Michael Hudson <m...@python.net> writes:

> There's define-symbol-macro, too. Don't think CMUCL supports it (but
> could be wrong).

Support for define-symbol-macro was added to CMU CL sometime before
18c was released.

Regs, Pierre.

--
Pierre R. Mai <pm...@acm.org> http://www.pmsf.de/pmai/
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents. -- Nathaniel Borenstein

0 new messages