练习2.21-练习2.35

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2014/01/20/2014-01-20-练习2.21-练习2.35/

访问原文「练习2.21-练习2.35

1.练习2.21

1
2
3
4
5
6
7
(define (square-list items)
(if (null? items)
'()
(cons (square (car items)) (square-list (cdr items)))))
(define (square-list items)
(map (lambda (x) (square x)) items))

2.练习2.22
3.练习2.23

1
2
3
4
5
6
7
8
(define (for-each f items)
(define (iter f items)
(if (null? items)
true
(begin
(f (car items))
(iter f (cdr items)))))
(iter f items))

4.练习2.24
5.练习2.25

1
2
3
4
5
(car (cdr (car (cdr (cdr (list 1 3 (list 5 7) 9))))))
(car (car (list (list 7))))
(car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7))))))))))))))))))

6.练习2.26

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

7.练习2.27

1
2
3
4
5
6
7
8
9
10
(define (deep-reverse items)
(define (reverse-iter tmpitems result)
(if (null? tmpitems)
result
(reverse-iter (cdr tmpitems)
(cons (if (number? (car tmpitems))
(car tmpitems)
(reverse-iter (car tmpitems) '()))
result))))
(reverse-iter items '()))

8.练习2.28

1
2
3
4
(define (fringe trees)
(cond ((null? trees) '())
((not (pair? trees)) (list trees))
(else (append (fringe (car trees)) (fringe (cdr trees))))))

9.练习2.29

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
(define (left-branch _mobile)
(car _mobile))
(define (right-branch _mobile)
(car (cdr _mobile)))
(define (branch-length _branch)
(car _branch))
(define (branch-structure _branch)
(car (cdr _branch)))
(define (total-weigh _mobile)
(if (not (pair? _mobile))
_mobile
(+ (total-weigh (branch-structure (left-branch _mobile)))
(total-weigh (branch-structure (right-branch _mobile))))))
(define (balance-weight _mobile)
(define (iter _mobile result)
(if (not (pair? _mobile))
true
(and result
(= (* (branch-length (left-branch _mobile))
(total-weigh (branch-structure (left-branch _mobile))))
(* (branch-length (right-branch _mobile))
(total-weigh (branch-structure (right-branch _mobile)))))
(balance-weight (branch-structure (left-branch _mobile)))
(balance-weight (branch-structure (right-branch _mobile))))))
(iter _mobile true))

10.练习2.30

1
2
3
4
5
6
7
8
9
10
11
12
(define (square-tree tree)
(cond ((null? tree) '())
((not (pair? tree)) (square tree))
(else (cons (square-tree (car tree))
(square-tree (cdr tree))))))
(define (square-tree tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree sub-tree)
(square sub-tree)))
tree))

11.练习2.31

1
2
3
4
5
6
(define (tree-map f tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map f sub-tree)
(f sub-tree)))
tree))

12.练习2.32

1
2
3
4
5
6
7
```plain
(define (subsets s)
(if (null? s)
(list '())
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (x) (cons (car s) x)) rest)))))

13.练习2.33

1
2
3
4
5
6
7
8
9
10
11
(define (map p sequence)
(accumulate (lambda (x y)
(cons (p x) y))
'()
sequence))
(define (append seq1 seq2)
(accumulate cons seq2 seq1))
(define (length sequence)
(accumulate (lambda (x y) (+ 1 y)) 0 sequence))

14.练习2.34

1
2
3
4
(define (horner-eval x coefficient-sequence)
(accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms)))
0
coefficient-sequence))

15.练习2.35

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(define (count-leaves tree)
(accumulate +
0
(map (lambda (sub-tree)
(if (pair? sub-tree)
(count-leaves sub-tree)
1))
tree)))
(define (count-leaves t)
(accumulate (lambda (x y) (if (not (pair? x))
(+ x y)
(+ (count-leaves x)
y)))
0
(map (lambda (x) (if (not (pair? x))
1
x))
(append (filter (lambda (x) (pair? x)) t)
(filter (lambda (x) (not (pair? x))) t)))))

```

Jerky Lu wechat
欢迎加入微信公众号