Showing posts with label lisp symbol. Show all posts
Showing posts with label lisp symbol. Show all posts

Thursday, May 14, 2009

Symbols are big - lexical variables

Ansi Common Lisp 8.5 Symbols and Variables

For a foo: 

(defun foo () '(1))
And evaluate:

A symbol used as a lexical variable is just a place holder: 

(symbol-name x) ; x is 'FOO

We can get name of 'x too: (symbol-name 'x) ; "X"

While other ops like (symbol-value 'x) and (symbol-function 'x) will yield error.

That's the difference between special variable and lexical variable: 
 * special variable will hold the symbol assigned to it
 * lexical variable will act as a place holder - only hold the name, and left the value-binding to the evaluator.


Symbols are big

Symbol structure from Chap 8 of Ansi Common Lisp.




So when a function FOO is defined, there will be a pair inserted into current package: <"FOO" 'FOO>, a string as function name and a symbol to hold the name, function object, package etc.

Unless FOO is in function call form (foo), you'll have to use symbol 'foo to reference it:

 When we set a variable to an old symbol, a new symbol is created and the value is set to the old symbol - kind of indirect pointers: