previous | index | next

More About append

As with all list-processing procedures we have seen so far, append does not harm any of its arguments:
     (define list1 '(1 2 3 4 5 6 7 8))

     (define list2 '(9 10))

     (append list1 list2) ⇒ (1 2 3 4 5 6 7 8 9 10)

     list1 ⇒ (1 2 3 4 5 6 7 8)

     list2 ⇒ (9 10)
Thus append re-creates (copies) list1 when building the new list.

append can be defined as:

     (define append
       (lambda (list1 list2)
         (if (null? list1)
             list2
             (cons (car list1)
                   (append (cdr list1) list2)))))

previous | index | next