博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SICP学习笔记 2.2.2 层次性结构
阅读量:4029 次
发布时间:2019-05-24

本文共 4949 字,大约阅读时间需要 16 分钟。

    练习2.24

;; 嵌套结构的list1 ]=> (list 1 (list 2 (list 3 4)));Value : (1 (2 (3 4))) */ \1  * /   \ 2    *     /   \    3   4

 

    练习2.25

;; (1 3 (5 7) 9)1 ]=> (define list1 (list 1 3 (list 5 7) 9))1 ]=> list1;Value : (1 3 (5 7) 9)1 ]=> (car (cdr (car (cdr (cdr list1)))));Value: 7;; ((7))1 ]=> (define list1 (list (list 7)))1 ]=> list1;Value : ((7))1 ]=> (car (car list1));Value: 7;; (1 (2 (3 (4 (5 (6 7))))))1 ]=> (define list1 (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))1 ]=> list1;Value : (1 (2 (3 (4 (5 (6 7))))))1 ]=> (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr list1))))))))))));Value: 7
 

     练习2.26

1 ]=> (append x y);Value : (1 2 3 4 5 6)1 ]=> (cons x y);Value : ((1 2 3) 4 5 6)1 ]=> (list x y);Value : ((1 2 3) (4 5 6))

    

    练习2.27

(define (deep-reverse items)  (cond ((null? items) '())	      ((pair? (car items))	       (append (deep-reverse (cdr items))		             (list (deep-reverse (car items)))))	      (else	       (append (deep-reverse (cdr items))		             (list (car items))))))
 

    练习2.28

;; 采用递归的方式,如果是序对就将左子树和右子树的结果拼接,否则直接拼接(define (fringe items)  (define (frings-iter things answer)    (cond ((null? things) answer)	        ((pair? things)	         (append (frings-iter (car things) answer)		               (frings-iter (cdr things) answer)))	        (else	         (append answer (list things)))))  (frings-iter items '()))
 

    练习2.29

;; a(define (left-branch mobile)  (car mobile))(define (right-branch mobile)  (cdr mobile))(define (branch-length branch)  (car branch))(define (branch-structure branch)  (car (cdr branch)))  ;; b;; 先检查是不是二叉活动体,如果是则递归求两个分支的重量;; 再检查分支的structure是否仍然是活动体, 如果是则递归求structure的重量;; 最后对于最简单的分支情况直接相加重量(define (total-weight mobile)  (define (mobile-flag m)    (pair? (left-branch m)))  (define (branch-flag m)    (pair? (branch-structure m)))  (define (total-weight-iter m tw)    (cond ((null? m) tw)	        ((mobile-flag m) (+ (total-weight-iter (left-branch m) tw)			                        (total-weight-iter (right-branch m) tw)))	        ((branch-flag m) (+ (total-weight-iter (branch-structure m) tw)))	        (else (+ tw (branch-structure m)))))  (total-weight-iter mobile 0));; 或者可以将活动体的重量看做是两个分支重量之和;; 但是在对分支求重量时仍然要区分是否还有分支(define (total-weight mobile)  (if (pair? (left-branch mobile))      (+ (branch-weight (left-branch mobile)) 	       (branch-weight (right-branch mobile)))      (branch-weight mobile))) (define (branch-weight branch)  (let ((structure	       (if (null? (right-branch branch))	           (left-branch branch)	           (branch-structure branch))))    (if (pair? structure)	      (branch-weight structure)	      structure)));; c;; 首先定义分支的力矩,依据其是否有分支分别处理(define (branch-value branch)  (if (null? (right-branch branch))      (* (branch-length (left-branch branch)) (branch-weight branch))      (* (branch-length branch) (branch-weight branch))));; 然后实现活动体的检测过程:两个分支平衡且两个分支的力矩相等(define (check-balance mobile)  (if (pair? (left-branch mobile))      (and (check-balance (left-branch mobile))	         (check-balance (car (right-branch mobile)))	         (= (branch-value (left-branch mobile))	            (branch-value (right-branch mobile))))      #t))  ;; d;; 需要对structure过程修改(define (branch-structure branch)  ;;(car (cdr branch)))  (cdr branch))  (define (check-balance mobile)  (if (pair? (left-branch mobile))      (and (check-balance (left-branch mobile))	         ;;(check-balance (car (right-branch mobile)))	         (check-balance (right-branch mobile))	         (= (branch-value (left-branch mobile))	            (branch-value (right-branch mobile))))      #t));; 验证  (define mtest   (make-mobile (make-branch 3 4)			         (make-branch 2 6)))1 ]=> (total-weight mtest);Value: 10			         		1 ]=> (check-balance mtest);Value: #t
 

    练习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 (map-square-tree tree)  (map (lambda (sub-tree)	        (if (pair? sub-tree)	            (map-square-tree sub-tree)	            (square sub-tree)))       tree))       1 ]=> (square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)));Value : (1 (4 (9 16) 25) (36 49))1 ]=> (map-square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)));Value : (1 (4 (9 16) 25) (36 49))

 

    练习2.31

(define (tree-map fun tree)  (map (lambda (sub-tree)      	 (if (pair? sub-tree)      	     (tree-map fun sub-tree)      	     (fun sub-tree)))       tree))       1 ]=> (define (square-tree-test tree) (tree-map square tree));Value : square-tree-test1 ]=> (square-tree-test (list 1 (list 2 (list 3 4) 5) (list 6 7)));Value : (1 (4 (9 16) 25) (36 49))
 

    练习2.32

;; 仿照换零钱的例子;; rest取不包含(car s)元素的所有剩余元素的组合;; 则应加上(car s)元素与所有剩余元素的组合(define (subsets s)  (if (null? s)      (list '())      (let ((rest (subsets (cdr s))))	      (append rest (map (lambda (r) (append (list (car s)) r)) rest)))))	1 ]=> (subsets s);Value : (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
 

 

 

 

转载地址:http://wovbi.baihongyu.com/

你可能感兴趣的文章
关于let{a}=B出现的解构赋值
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming course_2_assessment_1
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming Tuple Assignment with Unpacking
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming 排序函数sorted的可选参数
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
visca接口转RS-232C接口线序
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>