灯下 登录
计算机科学 / SICP / 3.3.1 Mutable List Structure

Exercise 3.13 · 习题

Exercise 3.13: Consider the following
make-cycle procedure, which uses the last-pair procedure defined
in Exercise 3.12:

(define (make-cycle x)
(set-cdr! (last-pair x) x)
x)

Draw a box-and-pointer diagram that shows the structure z created by

(define z (make-cycle (list 'a 'b 'c)))

What happens if we try to compute (last-pair z)?

练习 3.13:考虑以下 make-cycle 过程,它使用了练习 3.12 中定义的 last-pair 过程:

(define (make-cycle x)
(set-cdr! (last-pair x) x)
x)

画出由以下语句创建的结构 z 的序对指针图:

(define z (make-cycle (list 'a 'b 'c)))

如果我们试图计算 (last-pair z),会发生什么?

Racket #lang sicp
(define (make-cycle x)
 (set-cdr! (last-pair x) x)
 x)
Racket #lang sicp
(define z (make-cycle (list 'a 'b 'c)))