乐正

Actions speak louder than words.

Sicp-ex2-25

问题

给出能够从下面各表中取出7的carcdr组合:

1
2
3
4
5
(1 3 (5 7) 9)

((7))

(1 (2 (3 (4 (5 (6 7))))))

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;; (1 3 (5 7) 9)
(define x (list 1 3 (list 5 7) 9))
;Value: x

(car (cdr (car (cdr (cdr x)))))
;Value: 7

;; ((7))
(define x (list (list 7)))
;Value: x

(car (car x))
;Value: 7

;; (1 (2 (3 (4 (5 (6 7))))))
(define x (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
;Value: x

(car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr x))))))))))))
;Value: 7

draft

« sicp-ex2-24 sicp-ex2-26 »

Comments