sicp 习题 2.26 ~ 2.30

本文解析了Scheme编程语言中几个经典习题的实现方法,包括列表操作、树结构处理及复杂数据结构的递归操作。通过具体例子展示了如何利用递归进行列表反转、深反转以及树形结构的平方操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.26

(define x (list 1 2 3))
(define y (list 4 5 6))
(append x y)
(cons x y)
(list x y)

(define z (list x y))

(car z)
(cdr z)


2.27

(define nil '())

(define (my-reverse x)
(if (null? x)
nil
(append (my-reverse (cdr x)) (list (car x)))))

(define (deep-reverse lst)
(define (iter lst result)
(cond ((null? lst) result)
((not (pair? (car lst))) (iter (cdr lst) (cons (car lst) result)))
(else (iter (cdr lst) (cons (deep-reverse (car lst)) result)))))
(iter lst nil))

(define x (list (list 1 2) (list 3 4 5) (list 6 7) 8))
(define y (list 1 2))
(my-reverse x)
(deep-reverse x)


2.28

(define nil '())

(define x (list (list 1 2) (list 3 4)))

(define y (list x x))

(define (fringe x)
(define iter (lambda (x)
(append (fringe (car x)) (fringe (cdr x)))))

(cond ((null? x) nil)
((not (pair? x)) (list x))
(else (iter x))))

(fringe x)

(newline)

(fringe y)


2.29

(define (make-mobile left right)
(list left right))

(define (make-branch length structure)
(list length structure))

(define (left-branch m)
(car m))

(define (right-branch m)
(car (cdr m)))

(define (branch-length b)
(car b))

(define (branch-structure b)
(car (cdr b)))

(define l-branch (make-branch (make-branch 2 2) (make-branch 2 2)))

(define r-branch (make-branch (make-branch 2 2) (make-branch 2 2)))

(define mobile (make-mobile l-branch r-branch))

(left-branch mobile)

(right-branch mobile)

(define (is-mobile? m)
(and (pair? m)
(pair? (left-branch m))
(pair? (right-branch m))))

(define (total-weight m)
(if (is-mobile? m)
(+ (total-weight (left-branch m)) (total-weight (right-branch m)))
(branch-structure m)))

(define (total-moment m)
(if (is-mobile? m)
(+ (total-moment (left-branch m)) (total-moment (right-branch m)))
(* (branch-length m) (branch-structure m))))

(total-weight mobile)

(total-moment mobile)

(define (is-mobile-deep-than-2-level? m)
(and (pair? m)
(pair? (left-branch m))
(pair? (right-branch m))
(pair? (left-branch (left-branch m)))
(pair? (right-branch (left-branch m)))
(pair? (left-branch (right-branch m)))
(pair? (right-branch (right-branch m)))))

(is-mobile-deep-than-2-level? mobile)

(define (is-banlanced? m)
(if (is-mobile-deep-than-2-level? m)
(and (is-banlanced? (left-branch m)) (is-banlanced? (right-branch m)))
(= (total-moment (left-branch m)) (total-moment (right-branch m)))))


2.29 cons

(define (make-mobile left right)
(cons left right))

(define (make-branch length structure)
(cons length structure))

(define (left-branch m)
(car m))

(define (right-branch m)
(cdr m))

(define (branch-length b)
(car b))

(define (branch-structure b)
(cdr b))

(define l-branch (make-branch (make-branch 2 2) (make-branch 2 2)))

(define r-branch (make-branch (make-branch 2 2) (make-branch 2 2)))

(define mobile (make-mobile l-branch r-branch))

(left-branch mobile)

(right-branch mobile)

(define (is-mobile? m)
(and (pair? m)
(pair? (left-branch m))
(pair? (right-branch m))))

(define (total-weight m)
(if (is-mobile? m)
(+ (total-weight (left-branch m)) (total-weight (right-branch m)))
(branch-structure m)))

(define (total-moment m)
(if (is-mobile? m)
(+ (total-moment (left-branch m)) (total-moment (right-branch m)))
(* (branch-length m) (branch-structure m))))

(total-weight mobile)

(total-moment mobile)

(define (is-mobile-deep-than-2-level? m)
(and (pair? m)
(pair? (left-branch m))
(pair? (right-branch m))
(pair? (left-branch (left-branch m)))
(pair? (right-branch (left-branch m)))
(pair? (left-branch (right-branch m)))
(pair? (right-branch (right-branch m)))))

(is-mobile-deep-than-2-level? mobile)

(define (is-banlanced? m)
(if (is-mobile-deep-than-2-level? m)
(and (is-banlanced? (left-branch m)) (is-banlanced? (right-branch m)))
(= (total-moment (left-branch m)) (total-moment (right-branch m)))))


2.30

(define nil '())

(define (square x)
(* x x))

(define (square-tree tree)
(cond ((null? tree) nil)
((not (pair? tree)) (square tree))
(else (cons (square-tree (car tree)) (square-tree (cdr tree))))))

(define (square-tree-map tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree-map sub-tree)
(square sub-tree)))
tree))

(square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)))

(square-tree-map (list 1 (list 2 (list 3 4) 5) (list 6 7)))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值