Page 1 of 1

newbie question - plists and defvar

Posted: Sat Oct 13, 2012 8:29 pm
by alexakarpov
Hi all. My first post here (hopefully not the last), so please be patient!

I've set out to learn Lisp (for great good, no less), and started with this "Practical Common Lisp" book (hence question here). But one of the very first examples made me confused. It's about plists.

This works:

CL-USER> (getf (list :a 1 :b 2) :a)
1

But this does not:

CL-USER> (defvar foo (list :a 1 :b 2))
FOO
CL-USER> (getf foo :a)

It gives me an error:

value (1 2 3) is not of the expected type (SATISFIES
CCL::PLISTP).
[Condition of type TYPE-ERROR]

Indeed, when I check:

CL-USER> foo
(1 2 3)

Where are my keyword symbols?!

I did try googling, but nothing on the first page looked like an answer to my question... perhaps I am too clueless at this point to find an answer on my own!

Thanks!

Re: newbie question - plists and defvar

Posted: Sat Oct 13, 2012 11:27 pm
by edgar-rft
The ANSI CL Specification wrote: DEFPARAMETER and DEFVAR establish name as a dynamic variable. DEFPARAMETER unconditionally assigns the initial-value to the dynamic variable named name. DEFVAR, by contrast, assigns the initial-value (if supplied) to the dynamic variable named name only if name is not already bound.
The only possible explanation is that there already was the SYMBOL-VALUE (1 2 3) bound to the symbol FOO before DEFVAR, in this case DEFVAR does nothing. Use DEFPARAMETER if you want to redefine the SYMBOL-VALUE of arbitrary bound or unbound Common Lisp symbols.

- edgar

Re: newbie question - plists and defvar

Posted: Sun Oct 14, 2012 6:59 am
by alexakarpov
You are correct, of course. In fact, some 10 minutes after the posting I went back to reading and this time saw the "only if not already bound" part clearly...