Next: Association Lists, Previous: Accessing List Elements, Up: Lists [Contents][Index]
The nthcdr
function can be used in conjunction with the rplaca
function to modify an arbitrary element in a list. For example,
(rplaca (nthcdr 2 '(0 1 2 3 4 5)) 'foo) ⇒ foo
sets the third element of the list (0 1 2 3 4 5)
to the symbol
called foo
.
There are also functions which modify the structure of a whole list. These are called destructive operations because they modify the actual structure of a list—no copy is made. This can lead to unpleasant side effects if care is not taken.
This function is the destructive equivalent of the function append
,
it modifies its arguments so that it can return a list which is the
concatenation of the elements in its arguments lists.
Like all the destructive functions this means that the lists given as
arguments are modified (specifically, the cdr of their last cons cell
is made to point to the next list). This can be seen with the
following example (similar to the example in the append
documentation).
(setq foo '(1 2)) ⇒ (1 2) (setq bar '(3 4)) ⇒ (3 4) (setq baz (nconc foo bar)) ⇒ (1 2 3 4) foo ⇒ (1 2 3 4) ;`foo' has been altered! (eq (nthcdr 2 baz) bar) ⇒ t
The following diagram shows the final state of the three variables more clearly,
foo--> bar--> baz--> +-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+ | o | o----> | o | o----> | o | o----> | o | | +--|--+-----+ +--|--+-----+ +--|--+-----+ +--|--+-----+ | | | | --> 1 --> 2 --> 3 --> 4
This function rearranges the cons cells constituting the list list so that the elements are in the reverse order to what they were.
(setq foo '(1 2 3)) ⇒ (1 2 3) (nreverse foo) ⇒ (3 2 1) foo ⇒ (1) ;`foo' wasn't updated when the list ; was altered.
This function destructively removes all elements of the list list
which are equal
to object then returns the modified list.
(delete 1 '(0 1 0 1 0)) ⇒ (0 0 0)
When this function is used to remove an element from a list which is stored
in a variable that variable must be set to the return value of the
delete
function. Otherwise, if the first element of the list
has to be deleted (because it is equal
to object) the value
of the variable will not change.
(setq foo '(1 2 3)) ⇒ (1 2 3) (delete 1 foo) ⇒ (2 3) foo ⇒ (1 2 3) (setq foo (delete 1 foo)) ⇒ (2 3)
This function is similar to the delete
function, the only difference
is that the eq
function is used to compare object with each
of the elements in list, instead of the equal
function which
is used by delete
.
Destructively sorts (i.e. by modifying cdrs) the list of values
list, to satisfy the function predicate, returning the
sorted list. If predicate is undefined, the <
function is
used, sorting the list into ascending order.
predicate is called with two values, it should return true if the first is considered less than the second.
(sort '(5 3 7 4)) ⇒ (3 4 5 7)
The sort is stable, in that elements in the list which are equal will preserve their original positions in relation to each other.
See also delete-if
, delete-if-not
, filter
and so on.
(see Mapping Functions).
Next: Association Lists, Previous: Accessing List Elements, Up: Lists [Contents][Index]