这一节难度开始加大,有一些答案是借鉴了别人才做出来的。
;; 2.25
(define list1 (list 1 3 (list 5 7) 9))
;(car (cdr (car (cdr (cdr list1)))))
(define list2 (list (list 7)))
;(car (car list2))
(define list3 (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
;(car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr list3))))))))))))
;; 2.26
(define x (list 1 2 3))
(define y (list 4 5 6))
;(append x y)
;(cons x y)
;(list x y)
;; 2.27
(define (deep-reverse items)
(if (null? items) nil
(append (deep-reverse (cdr items))
(cons (if (pair? (car items)) (deep-reverse (car items))
(car items))
nil))))
(define x (list (list 1 2) (list 3 4)))
;(deep-reverse x)
(define (count-leaves x)
(cond ((null? x) 0)
((not (pair? x)) 1)
(else (+ (count-leaves (car x))
(count-leaves (cdr x))))))
;; 2.28
(define (fringe items)
(cond ((null? items) nil)
((pair? items) (append (fringe (car items))
(fringe (cdr items))))
(else (list items))))
(define x (list (list 1 2) (list 3 4)))
;(fringe x)
;(fringe (list x x))
;; 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 (make-mobile left right) (cons left right))
(define (make-branch length structure) (cons length structure))
(define (left-branch mobile) (car mobile))
(define (right-branch mobile) (cdr mobile))
(define (branch-length branch) (car branch))
(define (branch-structure branch) (cdr branch))
(define (mobile? structure) (pair? structure))
(define (branch-weight branch)
(let ((structure (branch-structure branch)))
(if (mobile? structure) (total-weight structure)
structure)))
(define (total-weight mobile)
(+ (branch-weight (left-branch mobile))
(branch-weight (right-branch mobile))))
(define (weight branch)
(* (branch-length branch) (branch-weight branch)))
(define (balance? mobile)
(let ((left (left-branch mobile))
(right (right-branch mobile)))
(and (= (weight left) (weight right))
(if (mobile? (branch-structure left)) (balance? left) #t)
(if (mobile? (branch-structure right)) (balance? right) #t))))
分享到:
相关推荐
SICP 习题答案 计算机程序的构造和解释 1-3章 习题答案
NULL 博文链接:https://pengpeng.iteye.com/blog/1344689
SICP习题解答,主要第一章的内容习题答案
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
sicp 2.2.4节图形语言的racket程序包,配置路径,C:\Users\Administrator\AppData\Roaming\Racket
Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用
sicp 2ed高清pdf,以及相对应的mit课程资料及习题答案打包,中文版的视频在这里http://i.youku.com/i/UNTcxODk3ODQw/videos?spm=a2hzp.8244740.0.0
SICP 解题集
SICP CHINESE ENGLISH THE SECOND EDITION SICP CHINESE ENGLISH THE SECOND EDITION
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 download : https://github.com/wizardforcel/sicp-py-zh
SICP_章_1-3 我对SICP第二版第1-3章的解决方案: 书中练习来自示例分配/ psets 项目位于 最后,来自考试
资源来自pypi官网。 资源全名:sicp-0.0.1b102.dev4.tar.gz
sicp-in-python(中文版+英文版)PDF 背景. SICP 全称Structure and Interpretation of Computer Programs,翻译过来叫《计算机程序的构造和解释》使用python
SICP 解题集《计算机程序的构造和解释》练习题解集。安装 MIT/GNU Scheme(macOS)下载:下载后运行 .dmg 文件,把 MIT/GNU Scheme.app 拖入 Applications 文件夹在 Applications/应用程序 文件夹中找到 MIT/GNU ...
资源名称:sicp 和 操作系统:精髓与设计原理第七版资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
经典书籍《计算机程序的构造与解释》,UCB热门课程CS61a的官方教材