newbie question - plists and defvar

Discussion of Common Lisp

newbie question - plists and defvar

Postby alexakarpov » Sat Oct 13, 2012 8:29 pm

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!
alexakarpov
 
Posts: 2
Joined: Sat Oct 13, 2012 8:15 pm

Re: newbie question - plists and defvar

Postby edgar-rft » Sat Oct 13, 2012 11:27 pm

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
edgar-rft
 
Posts: 155
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: newbie question - plists and defvar

Postby alexakarpov » Sun Oct 14, 2012 6:59 am

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...
alexakarpov
 
Posts: 2
Joined: Sat Oct 13, 2012 8:15 pm


Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests