CS 22

Clab 19: Mutable Lists


Review the material on pp. 252-255. http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-22.html#%_sec_3.3.1

Then do exs 3.12, 3.13, 3.14. Keep your brain working and make sure you understand what is happening. I'll be around to answer any questions. When you get to 3.13, remember the break button just below the menubar in descheme. Here is the code in those exercises in case you want to cut and paste.

;; EXERCISE 3.12 (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) (define (append! x y) (set-cdr! (last-pair x) y) x) (define (last-pair x) (if (null? (cdr x)) x (last-pair (cdr x)))) ;: (define x (list 'a 'b)) ;: (define y (list 'c 'd)) ;: (define z (append x y)) ;: z ;: (cdr x) ;: ;: (define w (append! x y)) ;: w ;: (cdr x) ;; EXERCISE 3.13 (define (make-cycle x) (set-cdr! (last-pair x) x) x) ;: (define z (make-cycle (list 'a 'b 'c))) ;; EXERCISE 3.14 (define (mystery x) (define (loop x y) (if (null? x) y (let ((temp (cdr x))) (set-cdr! x y) (loop temp x)))) (loop x '()))

Ask any questions you may have.