# 练习2.21-练习2.35

1.练习2.21

(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

(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

(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 4 5 6)
((1 2 3) 4 5 6)
((1 2 3) ( 4 5 6))

7.练习2.27

(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

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


9.练习2.29

(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

(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

(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

(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

(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

(define (horner-eval x coefficient-sequence)
(accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms)))
0
coefficient-sequence))
15.练习2.35

(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)))))



02-28 458
01-07 1161
12-23 1990
01-22 7958
04-15 97
11-27 4万+
09-07 379
08-19 514
05-21 5914
03-26 3048
06-24 493
08-02 340
06-27 323

### “相关推荐”对你有帮助么？

•  非常没帮助
•  没帮助
•  一般
•  有帮助
•  非常有帮助 被折叠的  条评论 为什么被折叠? 到【灌水乐园】发言   _海阔天空

¥1 ¥2 ¥4 ¥6 ¥10 ¥20 获取中  扫码支付 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。 余额充值