The following procedure for appending lists was introduced in section 2.2.1 :
(define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y))))
forms a new list by successively consing the elements of
. The procedure
is similar to
, but it is a mutator rather than a constructor. It appends the lists by splicing them together, modifying the final pair of
so that its
. (It is an error to call
with an empty
(define (append! x y) (set-cdr! (last-pair x) y) x)
is a procedure that returns the last pair in its argument:
(define (last-pair x) (if (null? (cdr x)) x (last-pair (cdr x))))
Consider the interaction
(define x (list 'a 'b)) (define y (list 'c 'd)) (define z (append x y)) z (a b c d) (cdr x) <response> (define w (append! x y)) w (a b c d) (cdr x) <response>
What are the missing
s? Draw box-and-pointer diagrams to explain your answer.
Nobody's finished this exercise yet. You'll be the first!
There are no comments yet.
You must log in to post a comment.Login