[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A locative is a type of Lisp object used as a pointer to a cell. Locatives are inherently a more "low level" construct than most Lisp objects; they require some knowledge of the nature of the Lisp implementation. Most programmers will never need them.
A cell is a machine word that can hold a (pointer to a) Lisp object. For example, a symbol has five cells: the print name cell, the value cell, the function cell, the property list cell, and the package cell. The value cell holds (a pointer to) the binding of the symbol, and so on. Also, an array leader of length n has n cells, and an art-q array of n elements has n cells. (Numeric arrays do not have cells in this sense.) A locative is an object that points to a cell; it lets you refer to a cell, so that you can examine or alter its contents.
There are a set of functions that create locatives to cells; the functions are documented with the kind of object to which they create a pointer. See ap-1, ap-leader, car-location, value-cell-location, etc. The macro locf (see (locf-fun)) can be used to convert a form that accesses a cell to one that creates a locative pointer to that cell: for example,
(locf (fsymeval x)) ==> (function-cell-location x) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Either of the functions car and cdr (see (car-fun)) may be given a locative, and will return the contents of the cell at which the locative points.
For example, (car (value-cell-location x)) is the same as (symeval x) |
Similarly, either of the functions rplaca and rplacd may be used to store an object into the cell at which a locative points.
For example, (rplaca (value-cell-location x) y) is the same as (set x y) |
If you mix locatives and lists, then it matters whether you use car and rplaca or cdr and rplacd, and care is required. For example, the following function takes advantage of value-cell-location to cons up a list in forward order without special-case code. The first time through the loop, the rplacd is equivalent to (setq res ...); on later times through the loop the rplacd tacks an additional cons onto the end of the list.
(defun simplified-version-of-mapcar (fcn lst) (do ((lst lst (cdr lst)) (res nil) (loc (value-cell-location 'res))) ((null lst) res) (rplacd loc (setq loc (ncons (funcall fcn (car lst))))))) |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |