Confused ....

Discussion of Common Lisp
Post Reply
makia
Posts: 25
Joined: Tue Jul 22, 2008 2:37 am

Confused ....

Post by makia » Tue Jul 22, 2008 2:45 am

CL-USER> (defparameter a 0)
CL-USER> (defparameter b 0)
A
CL-USER> (defun test (a)
(print (symbol-value a)))
TEST
CL-USER> (test 'a)

A
A
CL-USER> (test 'b)

0
0

I dont get second example, it should see dynamic binding ... afik :)
Thanks

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Confused ....

Post by ramarren » Tue Jul 22, 2008 3:15 am

makia wrote:I dont get second example, it should see dynamic binding ... afik :)
But you do see dynamic binding, so what is confusing? In the second example a is bound to symbol b, and the symbol-value returns, which is a function, returns the dynamic binding of b. In the first example a is bound to a, and then symbol-value returns the value of a, which is a.

But I don't think that making function arguments special is a good idea, and I can't really imagine any case where this would be more useful than confusing. There is a reason for *a* convention after all...

makia
Posts: 25
Joined: Tue Jul 22, 2008 2:37 am

Re: Confused ....

Post by makia » Tue Jul 22, 2008 3:22 am

Ramarren wrote:
makia wrote:I dont get second example, it should see dynamic binding ... afik :)
But you do see dynamic binding, so what is confusing? In the second example a is bound to symbol b, and the symbol-value returns, which is a function, returns the dynamic binding of b. In the first example a is bound to a, and then symbol-value returns the value of a, which is a.

But I don't think that making function arguments special is a good idea, and I can't really imagine any case where this would be more useful than confusing. There is a reason for *a* convention after all...
yes, nevermind the convention, this was just test of my understanding of language
btw. dynamic binding of a is 0, not symbol a

death
Posts: 17
Joined: Sat Jun 28, 2008 1:44 am

Re: Confused ....

Post by death » Tue Jul 22, 2008 3:45 am

makia wrote:yes, nevermind the convention, this was just test of my understanding of language
btw. dynamic binding of a is 0, not symbol a
No. When you evaluate

Code: Select all

(test 'a)
the variable a gets bound to the symbol a. Then Lisp will inspect the binding of variable a to get the symbol a (i.e. evaluate a), and pass that to symbol-value, which will return the value in symbol a's value cell, which is the symbol a. After the function returns, the variable a will again be bound to 0. The description of what happens in the evaluation of

Code: Select all

(test 'b)
is analogous.

makia
Posts: 25
Joined: Tue Jul 22, 2008 2:37 am

Re: Confused ....

Post by makia » Tue Jul 22, 2008 4:13 am

ahh, i expiremented a little .... and got it ...
defparameter establish dynamic variable so "a" in (defun test (a) ...) is dynamic not lexical (that's is what confused me, i thought "a" is lexical inside the function)

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Confused ....

Post by ramarren » Tue Jul 22, 2008 4:54 am

makia wrote:ahh, i expiremented a little .... and got it ...
defparameter establish dynamic variable so "a" in (defun test (a) ...) is dynamic not lexical (that's is what confused me, i thought "a" is lexical inside the function)
As far as I know, there is no way to un-special (ie. turn dynamic variable into lexical one) a symbol, locally or otherwise. This is I believe considered a flaw in the language, but it doesn't really come up that often.

theclapp
Posts: 17
Joined: Wed Jul 16, 2008 11:05 am

Re: Confused ....

Post by theclapp » Tue Jul 22, 2008 6:29 am

Ramarren wrote:
makia wrote:ahh, i expiremented a little .... and got it ...
defparameter establish dynamic variable so "a" in (defun test (a) ...) is dynamic not lexical (that's is what confused me, i thought "a" is lexical inside the function)
As far as I know, there is no way to un-special (ie. turn dynamic variable into lexical one) a symbol, locally or otherwise. This is I believe considered a flaw in the language, but it doesn't really come up that often.
If you use the *special* convention, this won't be a problem. That's why I cringed (without even really realizing why, at the time) when makia said "nevermind the convention" and then smiled at "dynamic binding of a is 0". It's remarkably easy even for experienced Lispers to make that mistake, which is why that convention exists: to remind you that variable *so-and-so* is special.

This flaw is really hard to remedy, given CL's optional type declarations. If we had required type decs, you'd be required to redeclare it as special every time you bound it. But then we'd have required type decs, and, you know, yuck. Even then it could be confusing -- it's awfully easy to forget what C variables are global, for example. In C, many people add some kind of prefix to globals. That's all the *'s are, really. :)

"Special" variables are one of the few (only?) times in CL that a variable has a type (or, at least, a property), as opposed to only the value it's bound to having a type.

Post Reply