In terms of scope? Actual implementation in memory? The syntax? For eg, if (let a 1) Is ‘a’ a variable or a symbol?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Jörg’s answer points in the right direction. Let me add a bit to it.
I’ll talk about Lisps that are similar to Common Lisp.
Symbols as a data structure
A symbol is a real data structure in Lisp. You can create symbols, you can use symbols, you can store symbols, you can pass symbols around and symbols can be part of larger data structures, for example lists of symbols. A symbol has a name, can have a value and can have a function value.
So you can take a symbol and set its value.
Usually one would write
(setq foo 42), or(set 'foo 42)or(setf foo 42).Symbols in code denoting variables
But!
or
In both forms above in the source code there are symbols and
ais written like a symbol and using the functionREADto read that source returns a symbolain some list. But thesetqoperation does NOT set the symbol value ofato42. Here theLETand theDEFUNintroduce a VARIABLEathat we write with a symbol. Thus theSETQoperation then sets the variable value to42.Lexical binding
So, if we look at:
We introduce a global variable
FOO.In bar the first
SETQsets the symbol value of the global variableFOO. The secondSETQsets the local variableBAZto3. In both case we use the sameSETQand we write the variable as a symbol, but in the first case theFOOdonates a global variable and those store values in the symbol value. In the second caseBAZdenotes a local variable and how the value gets stored, we don’t know. All we can do is to access the variable to get its value. In Common Lisp there is no way to take a symbolBAZand get the local variable value. We don’t have access to the local variable bindings and their values using symbols. That’s a part of how lexical binding of local variables work in Common Lisp.This leads for example to the observation, that in compiled code with no debugging information recorded, the symbol
BAZis gone. It can be a register in your processor or implemented some other way. The symbolFOOis still there, because we use it as a global variable.Various uses of symbols
A symbol is a data type, a data structure in Lisp.
A variable is a conceptual thing. Global variables are based on symbols. Local lexical variables not.
In source code we write all kinds of names for functions, classes and variables using symbols.
There is some conceptual overlap:
In the above SOURCE code,
defun,foo,bar,setqandbazare all symbols.DEFUNis a symbol providing a macro.FOOis a symbol providing a function.SETQis a symbol providing a special operator.BAZis a symbol used as data. Thus the quote beforeBAZ.BARis a variable. In compiled code its symbol is no longer needed.