Global lexical variable?

38 views
Skip to first unread message

Graham Hughes

unread,
Jun 2, 1998, 3:00:00 AM6/2/98
to

I'm working my way through _On_Lisp_ (Paul Graham, 1994), and got to chapter
20 where he discusses continuation passing macros. Now, I understand all
about the macros and how they work, but his example does a

(setq *cont* #'identity)

to set up a global lexical variable, as opposed to the usual global special.

My compiler, Allegro CL, in its infinite wisdom decided I really wanted
*cont* to be special, and so does it for me. Normally, this is convenient,
but not now; the macros really depend on it being lexical.

How do I do this? Portable would be nice, because I'd like the utilities to
run on CMU CL as well.

(BTW, because of the way the macros work, wrapping all the defmacro and
whatnot in a giant let and doing things that way doesn't work. I tried.)

Graham
--
Graham Hughes <th...@treepeople.dyn.ml.org>
http://treepeople.dyn.ml.org/thrag/
PGP Fingerprint: 36 15 AD 83 6D 2F D8 DE EC 87 86 8A A2 79 E7 E6


Kent M Pitman

unread,
Jun 2, 1998, 3:00:00 AM6/2/98
to

gra...@A3162.resnet.ucsb.edu (Graham Hughes) writes:

>
> I'm working my way through _On_Lisp_ (Paul Graham, 1994), and got to
> chapter 20 where he discusses continuation passing macros. Now, I
> understand all about the macros and how they work, but his example
> does a
>
> (setq *cont* #'identity)
>
> to set up a global lexical variable, as opposed to the usual global
> special.

Yeah, I think this is undefined and implementations are free to
interpret it in several different ways. Something like the following
should work. I didn't test it, though. Sorry--you get what you pay
for...

(defmacro deflexical (var &optional init doc)
`(progn (define-symbol-macro ,var (get ',var 'lexical))
(setf ,var ,init)
(setf (get ',var 'lexical-doc) ',doc)
',var))


David Bakhash

unread,
Jun 3, 1998, 3:00:00 AM6/3/98
to

what is the meaning of a global lexical variable. I also remember
seeing mention of this in Graham's book, but he didn't get into it, as
far as I know. Can someone tell me of an instance in which this would
be useful? I guess I can imagine but I often think of indefinate
extent and global as the same thing.

dave

Kent M Pitman

unread,
Jun 3, 1998, 3:00:00 AM6/3/98
to

David Bakhash <ca...@bu.edu> writes:

It's a binding that's something you can close over and is thus not
interfered with by either lexical or special bindings.

That is,

(deflexical x 3)
(defun f () x)

Is the same as

(let ((x 3)) (defun f () x))

except that the scope of the x is global instead of local to a binding form.
So, for example:

(deflexical x 3)

(defun f () x) ;closed over x

(defun g (y)
(let ((x (+ y 1)))
(declare (special x))
(h (+ x 1))))

(defun h (x)
(list (f) (locally (declare (special x)) x) x))

(g x)
=> (3 4 5)

I was in a hurry and didn't test any of the above.
But it loks right to me...

Peter.VanEynde

unread,
Jun 8, 1998, 3:00:00 AM6/8/98
to

Graham Hughes <gra...@A3162.resnet.ucsb.edu> wrote:
: I'm working my way through _On_Lisp_ (Paul Graham, 1994), and got to chapter

: 20 where he discusses continuation passing macros. Now, I understand all
: about the macros and how they work, but his example does a

: (setq *cont* #'identity)

: to set up a global lexical variable, as opposed to the usual global special.

: My compiler, Allegro CL, in its infinite wisdom decided I really wanted


: *cont* to be special, and so does it for me. Normally, this is convenient,
: but not now; the macros really depend on it being lexical.

: How do I do this? Portable would be nice, because I'd like the utilities to
: run on CMU CL as well.

CMUCL will also make it special if you don't set some flag. Most of the
time it's the "right" thing to do.

Groetjes, Peter

--
It's logic Jim, but not as we know it. http://hipe.uia.ac.be/~s950045
Look in keyservers for PGP key.

Graham Hughes

unread,
Jun 9, 1998, 3:00:00 AM6/9/98
to

"Peter.VanEynde" <s95...@hhipe.uia.ac.be> writes:

>CMUCL will also make it special if you don't set some flag. Most of the
>time it's the "right" thing to do.

Granted; this is the only time I have ever had to do this. Kent Pitman
supplied a macro that should work everywhere (thanks again!).

Reply all
Reply to author
Forward
0 new messages