next up previous
Contents Next: Selectors: First and Up: Some Primitive Functions Previous: Constructors: ConsList,

Quote

The answer to the previous question is to use the single quote mark, ', in front of an item that you do not wish the interpreter to evaluate. It works as follows:

>'a
a
Without the quote you would have received an error message. So now you might try (cons 'a (b c d)), but this still won't work since the interpreter tries to evaluate the second argument (b c d), treating b as the name of a function. But (we are assuming) you have not defined b to be a function, so it is an error. Once again you can use the ' to block evaluation of the list (b c d). Thus:

>(cons 'a '(b c d))
(a b c d)
Notice that you need only one quote on the second argument. The quote blocks evaluation of the whole thing, so you don't need quotes on the internal elements of the list.

Notice, also, that in using setq, the first argument -- the variable to be set -- does not need a quote; the interpreter does not evaluate it if it is an atom. Originally, LISP had only the function set, whose proper use included things like (set 'a 4) or (set 'a 'b). LISP programmers got tired of putting (or, more likely, forgetting to put) the quote on the first argument, so they wrote setq to take care of it automatically (now you understand the name, right?!) So, (setq a 4) is equivalent to (set 'a 4).

In fact, the ' itself is shorthand for another function. The interpreter expands 'a to the expression (quote a). So (setq a 4) is really short for (set (quote a) 4). Shorthand commands like setq and ' are called ``macros'' by programmers.

Cons always takes two arguments. (Try it with one or three and see that you get an error message.) Usually, the second argument is a list, but the first can be either an atom or a list. (Cons can be used with an atom as the second argument. What gets returned is a slightly esoteric data type called a ``dotted pair.'' The use of dotted pairs is relatively rare and will be ignored here.)

The list building functions list and append both allow arbitrary numbers of arguments. List takes all its arguments and makes a list with them as elements. Arguments to list will typically be either atoms or lists. For example:

>(list 2 3 4)
(2 3 4)
>(list 'a '(a s d f))
(a (a s d f))
Make sure you understand why list works as shown in these examples.

Append is normally used with lists as arguments. (Only the last argument may be an atom, resulting in a dotted pair.) When used with lists, append has the effect of taking all the members of the lists that are its arguments and creating a new list from those elements. For example:

>(append '(a b) '(c d))
(a b c d)
Beginning LISP programmers (and even some experienced ones) frequently have trouble deciding whether they should use cons or list or append to achieve a particular effect. The best way to get around the difficulty is to play with the different functions and see what works.



next up previous
Contents Next: Selectors: First and Up: Some Primitive Functions Previous: Constructors: ConsList,



© Colin Allen & Maneesh Dhagat
November 1999