Elisp
Lisp stands for LISt Processing. Elisp is the dialect of Lisp used in emacs, a text editor.
In Lisp, both data and programs are represented the same way: they are both lists of words, numbers, or other lists, sparated by whitespace and surrounded by parentheses.
Parts of lisp
- Expression
- Lisp programs are made up of expressions: lists or single atoms
- Atom
- Atoms are multi-character symbols, like forward-paragraph, single character symbols like +, strings of characters between double quotation marks, or numbers. Arrays are also considered to be an atom. Any object that is not a Cons Cell is an atom.
- List
- Made up of zero or more atoms or inner lists, separated by whitespace and surrounded by parentheses. A list can be empty.
- String
- Between quotation marks
- Symbol
- Looks like a word. A name that can have a value attached to it just as it can have a function definition attached to it. It can have both at the same time. Or it can have just one or the other.
- Number
- Evaluates to itself.
- Function
- A set of instructions to the computer that tell the computer to do something
- Variable
- A symbol that is not quoted and that doesn't have parentheses around it. We bind a variable to a value.
- Special forms
- Some functions are unusual and do not work the usual manner. They are called special forms. Used for special jobs like defining a function, or 'if'.
- Quote
- A single-quote ‘'’ tells the Lisp interpreter that it should return the following expression as written, and not evaluate it as it would if the quote were not there.
- Macro
- A construct which differs from a function in that it translates a lisp expression into another expression that is to be evaluated in place of the original expression, e.g. 'when'.
- Argument (of a function)
- Information passed to the function. Atoms or lists that follow the function.
- Predicate
- A function to determine whether some preperty is true or false.
- Comment
- It starts with ;
- Interactive function
- A function we evaluate by typing M-x or through a key or keychord.
- Buffer
- Place where we do things. It's not a file, but it can be visiting a file.
- car (first)
- Reports what the first item in the list is.
- cdr (rest)
- The part of the list that follows the first item.
- Cons cell
- An object that consists of two slots, the car slot and the cdr slot. Each slot can hold any lisp object.
- List
- A series of cons cells, linked together so that the CDR slot of each cons cell holds either the next cons cell or the empty list.
- Dotted pair notation
- General syntax for cons cells that represents the CAR and CDR explicitly. (a . b) stands for a cons cell whose car is the object a and whose CDR is the object b.
- Association list
- Alist records a mapping from keys to values. It is a list of cons cells called associations. The CAR of each cons cell is the key, and the CDR is the associated value.
- Collection of one or more files, formatted and bundled in such a way that users can easily download, install, uninstall and upgrade it.
Evaluating parts of lisp
- A ' in front of a list is called a quote. It tells lisp to do nothing with the list other than take it as it is written.
- When there's no quote preceeding a list, it is a command for the computer to obey.
- When you evaluate a symbol by itself, its value is returned.
- When you evaluate a quoted variable (e.g. 'flower), what we will see in the echo area is the symbol itself, flower.
- The interpreter works on the innermost list first and the works on the outside list.
- Evaluating a symbolic expression most commonly causes the interpreter to return a value and perhaps carry out a side effect; or produce an error.
Useful functions
(message "this message appears in the echo area")
; we can use %d for integers, %s for strings(set 'flowers '(rose violet daisy buttercup))
; Set the value of the symbol flower to the list. It returns the value (list) and binds the symbol flower to the list as a side-effect.(setq flowers '(rose violet daisy buttercup))
special form that quotes the first argument automatically. Can set multiple values directly.(defun function-name (arguments...) "optional doc" (interactive) body...)
macro to define a function definition(let varlist body...)
special form to bind a symbol to a value. It creates a local variable.(if true-or-false-test then-part else-part)
special form for conditional.- cons, car, cdr, nthcdr, nth, setcar, setcdr...
(eval '(+ 2 2))
to evaluate a quoted expression.
Other things
- C-u allows to pass numeric arguments to interactive functions
- False is nil, anything else is true.
- Nil has two meanings: false and empty list.
- Quote returns data which you should not modify, and which may share structure between themselves. It's data part of the program. Do not change it!
- Lists and quotes are different. quote “distributes” over its arguments, so '(+ 1 2) is like (list '+ '1 '2)
Emacs specific functions
load-file
loads an elisp file.load-library
loads with a library name (installed in load-path)load
similar to load-library but lower-level. Same file can be loaded repeatedly.require
ensure the library is loaded once only.autoload
allows you to pre-load a function without installing it by specifing its name, its library file, documentation and if it should be interactive.
Backlinks:
journal notes watching serial experiments lain html journal emacs