[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
special variables
- To: <clisp-list@ma2s2.mathematik.uni-karlsruhe.de>
- Subject: special variables
- From: sshteingold@cctrading.com
- Date: Thu, 20 Nov 97 11:31:11 -0500
- Return-receipt-to: <sshteingold@cctrading.com>
Consider the following function:
(defun zz (level maxlev)
(format t "Level: ~d" level)
(let (x)
(declare (special x))
(when (zerop level) (setq x 10))
(format t "~20tx: ~a~%" x)
(when (> maxlev level) (zz (1+ level) maxlev))))
> (zz 0 3)
Level: 0 x: 10
Level: 1 x: nil
Level: 2 x: nil
Level: 3 x: nil
nil
>
First, note the same bug as before -- with ~t formatting of the first
line. Second, this is not quite what I want (although what one would
expect from the ANSI CL standard as well as from the good old common
sense). I want X to be the same variable in all the calls.
I am traversing a tree using recursion. But the tree is not really a
tree, in the sense that some nodes are similar, and I do not want to
study them twice. So, I would like to create a variable binding which
would be the same throughout all the calls, which will be accessed and
modified in all the calls. Essentially, this can be achieved with a
global variable, but this global variable will be a piece of junk this
is anti-aesthetical. I wish I could do something like
(when (zerop level) (defvar x))
........ recursion .....
(when (zerop level) (destroy x))
Another option is to pass the thing as a variable in each call:
(defun zz (level maxlev x)
(push level x)
(format t "Level: ~d~20tx: ~a~%" level x)
(when (> maxlev level) (zz (1+ level) maxlev x))
(format t "Level: ~d~20tx: ~a~%" level x))
> (zz 0 3 nil)
Level: 0 x: (0)
Level: 1 x: (1 0)
Level: 2 x: (2 1 0)
Level: 3 x: (3 2 1 0)
Level: 3 x: (3 2 1 0)
Level: 2 x: (2 1 0)
Level: 1 x: (1 0)
Level: 0 x: (0)
nil
>
this is *NOT* what I want. I want
> (zz 0 3 nil)
Level: 0 x: (0)
Level: 1 x: (1 0)
Level: 2 x: (2 1 0)
Level: 3 x: (3 2 1 0)
Level: 3 x: (3 2 1 0)
Level: 2 x: (3 2 1 0)
Level: 1 x: (3 2 1 0)
Level: 0 x: (3 2 1 0)
nil
>
Any suggestions?
(I guess, I'll go for a global var. Sad.)
Thanks.