Announcements
Final Exam
Johnson Athletic Center (not what the Registrar said!), Wed.
May 21, 1:30pm to 4:30pm.
Current Machine
Memory
Machine with Stack in Memory
Typed Data
Copying a List
(define (copy-list-1 l)
(cond ((null? l) '())
((pair? l)
(cons (copy-list-1 (car l))
(copy-list-1 (cdr l))))
(else l)))
(define x '(a b c))
(define y (cons x x))
y ; ((a b c) a b c)
(eq? (car y) (cdr y)) ; #T
(define z (copy-list-1 y))
z ; ((a b c) a b c)
(eq? (car z) (cdr z)) ; #F
Structure-Preserving Copy
(define (copy-list-2 l)
(let ((tag (list 'BEEN-HERE-DONE-THAT)))
(define (copy l)
(cond ((null? l) '())
((and (pair? l) (not (eq? (car l) tag)))
(let ((the-car (car l))
(the-cdr (cdr l))
(result (cons 0 0)))
(set-car! l tag)
(set-cdr! l result)
(set-car! result (copy the-car))
(set-cdr! result (copy the-cdr))
result))
((pair? l) (cdr l))
(else l)))
(copy l)))
(define z (copy-list-2 y))
z ; ((a b c) a b c)
(eq? (car z) (cdr z)) ; #T
(define x '(a b c))
(set-cdr! x x)
(define y (copy-list-2 x))
(eq? y (cdr y)) ; #T