Understanding Symbols and Variables
I was chatting with a friend of mine about some code that we are writing for a project and the discussion of symbol-value came up. I had a hard time explaining symbol-value mainly because I chose a wrong example to begin with. The best part about Common Lisp or any other interactive language is that you can try everything the moment you think about it.
(setf x 'a)
(setf (symbol-value x) 'b)
The two statements are the simplest explanation. The first is creating a global variable x and binding the value 'a to it. The second statement is more interesting because it lets me set the value that was retrived from x to something, in effect, creating a global variable -> a and binding the value of -> a to 'b.
(setf (symbol-value x) 'b) => (setf a b)
Because of the current binding of x. This feature of Lisp will let me assign a global dynamically. Feel the power? No ... check out the set-var-from-string macro here.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home