Node:Append/Reverse, Next:, Previous:List Selection, Up:Lists



22.2.5 Append and Reverse

append and append! are used to concatenate two or more lists in order to form a new list. reverse and reverse! return lists with the same elements as their arguments, but in reverse order. The procedure variants with an ! directly modify the pairs which form the list, whereas the other procedures create new pairs. This is why you should be careful when using the side-effecting variants.

append . args Scheme Procedure
scm_append (args) C Function
Return a list consisting of the elements the lists passed as arguments.
(append '(x) '(y))          =>  (x y)
(append '(a) '(b c d))      =>  (a b c d)
(append '(a (b)) '((c)))    =>  (a (b) (c))
The resulting list is always newly allocated, except that it shares structure with the last list argument. The last argument may actually be any object; an improper list results if the last argument is not a proper list.
(append '(a b) '(c . d))    =>  (a b c . d)
(append '() 'a)             =>  a

append! . lists Scheme Procedure
scm_append_x (lists) C Function
A destructive version of append (see Pairs and lists). The cdr field of each list's final pair is changed to point to the head of the next list, so no consing is performed. Return a pointer to the mutated list.

reverse lst Scheme Procedure
scm_reverse (lst) C Function
Return a new list that contains the elements of lst but in reverse order.

reverse! lst [new_tail] Scheme Procedure
scm_reverse_x (lst, new_tail) C Function
A destructive version of reverse (see Pairs and lists). The cdr of each cell in lst is modified to point to the previous list element. Return a pointer to the head of the reversed list.

Caveat: because the list is modified in place, the tail of the original list now becomes its head, and the head of the original list now becomes the tail. Therefore, the lst symbol to which the head of the original list was bound now points to the tail. To ensure that the head of the modified list is not lost, it is wise to save the return value of reverse!