Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between symbol and variable name in emacs lisp

Tags:

emacs

elisp

I'm wondering what the difference is between

(add-to-list 'flymake-allowed-file-name-masks
         '("\\.py\\'" flymake-pylint-init))

and

(add-to-list flymake-allowed-file-name-masks
         '("\\.py\\'" flymake-pylint-init))

What is the clear meaning of the apostrophe here?

like image 526
kjshim Avatar asked Nov 23 '09 02:11

kjshim


People also ask

What is a symbol in Lisp?

In LISP, a symbol is a name that represents data objects and interestingly it is also a data object. What makes symbols special is that they have a component called the property list, or plist.

What is the Emacs symbol?

A symbol in GNU Emacs Lisp is an object with a name. The symbol name serves as the printed representation of the symbol. In ordinary Lisp use, with one single obarray (see Creating and Interning Symbols), a symbol's name is unique—no two symbols have the same name.

Are there variables in Lisp?

There are actually two kinds of variables in Common Lisp, called lexical (or static) variables and special (or dynamic) variables. At any given time either or both kinds of variable with the same name may have a current value.

Is keyword a symbol?

The difference between symbols and keywords is that keywords evaluate to themselves, while symbols evaluate to whatever they are bound to. To have a symbol evaluate to itself, it must be quoted.


1 Answers

The apostrophe is a quote, which tells the interpreter to not parse the following expression (the symbol name). Thus, 'add-to-list gets the symbol which contains the list value that is intended to be evaluated.

To learn more about symbols, read the Symbol documentation (specifically, Symbol Components symbols have names, values, function definitions and property lists).

Without reading the documentation, this is how I explain it: Emacs lisp's evaluation strategy is to pass by value (as opposed to by name or reference or something else). If the quote were not there, flymake-allowed-file-name-masks would be evaluated to a value, and add-to-list would have to work directly on the list. This would work, with the following exceptions. If the list were empty, there would be no way to change what the original variable pointed to. For the same reason, you would not be able to add elements to the front of the list.

To make those two cases work, you actually need the variable name so that you can modify what it points to.

It'd probably be useful to read the following: Introduction to Evaluation, Modifying List Variables, and Modifying Existing List Structures.

If you're familiar with box diagrams, perhaps this will help.

Imagine that some-var points to a list:

somevar
  |
  |
  v
 --- ---      --- ---      --- ---        
|   |   |--> |   |   |--> |   |   |--> nil
 --- ---      --- ---      --- ---        
  |            |            |             
  |            |            |             
   --> rose     --> violet   --> buttercup

And you wanted to put something on the front of that list.

If all you have to work with is the value of the pointer in somevar, then the best you can do is put a new element on the front of the list, but you cannot actually modify what somevar points to (because you don't have somevar, you have it's value). Like so:

             somevar
               |
               |
               v
 --- ---      --- ---      --- ---      --- ---        
|   |   |--> |   |   |--> |   |   |--> |   |   |--> nil
 --- ---      --- ---      --- ---      --- ---        
  |            |            |            |             
  |            |            |            |             
   --> tulip     --> rose     --> violet   --> buttercup

So, to write your own 'add-to-list function, you need the variable name.

Of course, if you wrote 'add-to-list as a macro, you wouldn't have that restriction.

like image 112
Trey Jackson Avatar answered Sep 25 '22 07:09

Trey Jackson