From: Thaddeus L Olczyk
Subject: Tacking changes to a variable?
Date: 
Message-ID: <3c72be34.270186578@nntp.interaccess.com>
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?

From: Hannah Schroeter
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <a4ufaj$7fj$16@news.schlund.de>
Hello!

In article <··················@nntp.interaccess.com>,
Thaddeus L Olczyk <······@interaccess.com> wrote:
>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?

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.
From: Erik Naggum
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <3223146971133023@naggum.net>
* 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.

///
-- 
  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.
From: Rolf Wester
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <3C7369D5.BB45BFA1@ilt.fhg.de>
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
From: Rahul Jain
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <877kp8fq63.fsf@photino.sid.rice.edu>
Rolf Wester <······@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 -=-  ············@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.
From: Tim Bradshaw
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <ey3y9howisi.fsf@cley.com>
* 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
From: Kent M Pitman
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <sfweljgxuiy.fsf@shell01.TheWorld.com>
Rahul Jain <·····@sid-1129.sid.rice.edu> writes:

> Rolf Wester <······@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))))
From: Vladimir Zolotykh
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <3C738213.3914FF5A@eurocom.od.ua>
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
From: Michael Hudson
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <uwux8bdk9.fsf@python.net>
Vladimir Zolotykh <······@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
From: Pierre R. Mai
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <87n0y4yygw.fsf@orion.bln.pmsf.de>
Michael Hudson <···@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 <····@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
From: Kent M Pitman
Subject: Re: Tacking changes to a variable?
Date: 
Message-ID: <sfw3czw1iud.fsf@shell01.TheWorld.com>
Vladimir Zolotykh <······@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'. 
> 
> 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 ?

DEFINE-SYMBOL-MACRO does the global thing.