| Song's profileSICP自留地BlogListsNetwork | Help |
|
|
July 16 sicp 2.68;my work is between the two asterisk lines
(define (make-leaf symbol weight) (list 'leaf symbol weight)) (define (leaf? object) (eq? (car object) 'leaf)) (define (symbol-leaf x) (cadr x)) (define (weight-leaf x) (caddr x)) (define (make-code-tree left right)
(list left right (append (symbols left) (symbols right)) (+ (weight left) (weight right)))) (define (left-branch tree) (car tree))
(define (right-branch tree) (cadr tree)) (define (symbols tree)
(if (leaf? tree) (list (symbol-leaf tree)) (caddr tree))) (define (weight tree) (if (leaf? tree) (weight-leaf tree) (cadddr tree))) (define (decode bits tree)
(define (decode-1 bits current-branch) (if (null? bits) '() (let ((next-branch (choose-branch (car bits) current-branch))) (if (leaf? next-branch) (cons (symbol-leaf next-branch) (decode-1 (cdr bits) tree)) (decode-1 (cdr bits) next-branch))))) (decode-1 bits tree)) (define (choose-branch bit branch) (cond ((= bit 0) (left-branch branch)) ((= bit 1) (right-branch branch)) (else (error "bad bit -- CHOOSE-BRANCH" bit)))) ;********************* (define (element-of-set? x set) (cond ((null? set) #f) ((equal? x (car set)) #t) (else (element-of-set? x (cdr set))))) (define (encode message tree)
(if (null? message) '() (append (encode-symbol (car message) tree) (encode (cdr message) tree)))) (define (encode-symbol symbol tree);it would be better to apply some tree checking method here
(encode-symbol1 symbol '() (left-branch tree) (right-branch tree))) (define (encode-symbol1 symbol result left-b right-b)
(cond ((element-of-set? symbol (symbols left-b)) (if (leaf? left-b) (append result '(0)) (encode-symbol1 symbol (append result '(0)) (left-branch left-b) (right-branch left-b)))) ((element-of-set? symbol (symbols right-b)) (if (leaf? right-b) (append result '(1)) (encode-symbol1 symbol (append result '(1)) (left-branch right-b) (right-branch right-b)))) (else (error "encounter symbol could not be encoded with current dictionary:" symbol)))) ;*********************
(define sample-tree (make-code-tree (make-leaf 'A 4) (make-code-tree (make-leaf 'B 2) (make-code-tree (make-leaf 'D 1) (make-leaf 'C 1))))) (define sample-message '(0 1 1 0 0 1 0 1 0 1 1 1 0)) (define sample-symbols (decode sample-message sample-tree)) (encode sample-symbols sample-tree);test if my procedure could encode message right (encode '(A B S) sample-tree);test the error reporting func. sicp 2.65是想要告诉大家虽然平衡二叉树的索引效率较高,做集合运算还是将先其转换为序列比较有效率?应该是这样吧。 SICP 2.64以递归的方法将一序列转换成平衡二叉树的方法简要来说就是
1.找出序列中间的元素n0
2.将原序列以n0为分割,分割成不含n0的前方序列t1和后方t2
3.以n0为node,将序列t1和t2转换后的平衡二叉树分别作为其左右分支,从而构建出更大的平衡二叉树
具体实现方法可以有细微的区别,书中给出的partial-tree定义简洁优雅,相当强!
由于转换过程实际上遍例序列中所有元素一次,其步骤复杂度为theta(n),n为序列元素个数 July 13 sicp 2.59(define (union-set set1 set2)
(if (null? set2) set1 (union-set-good set1 set2 set2))) (define (union-set-good set1 result set2base) (if (null? set1) result (if (element-of-set? (car set1) set2base) (union-set-good (cdr set1) result set2base) (union-set-good (cdr set1) (cons (car set1) result) set2base)))) (define (element-of-set? x set) (cond ((null? set) #f) ((equal? x (car set)) #t) (else (element-of-set? x (cdr set))))) July 10 sicp 2.57;只改进加法部分,乘法类似
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2) ((=number? a2 0) a1) ((and (number? a1) (number? a2)) (+ a1 a2)) ((eq? (car a2) '+) (cons '+ (cons a1 (cdr a2))));though not necessary, I try to reduce the result to a simpler form (else (list '+ a1 a2)))) (define (=number? exp num) (and (number? exp) (= exp num))) (define (augend s)
(if (pair? (cdddr s));check if s havs more than 2 terms (cons '+ (cddr s)) (caddr s))) sicp 2.56(define (deriv exp var)
(cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) ((exponentiation? exp) ;what we derive here should be limited to certain combination (make-product (make-product (exponent exp) (make-exponent (base exp) (- (exponent exp) 1))) (deriv (base exp) var) )) (else (error "unknown expression type -- DERIV" exp)))) ;data abstraction layer
(define (variable? x) (symbol? x)) (define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2))) (define (make-sum a1 a2) (list '+ a1 a2))
(define (make-product m1 m2) (list '* m1 m2))
(define (sum? x)
(and (pair? x) (eq? (car x) '+))) (define (addend s) (cadr s))
(define (augend s) (caddr s))
(define (product? x)
(and (pair? x) (eq? (car x) '*))) (define (multiplier p) (cadr p))
(define (multiplicand p) (caddr p))
;my definition start here (define (exponentiation? x) (and (pair? x) (eq? (car x) '**))) (define (make-exponent b e)
(list '** b e)) (define (base e)
(cadr e)) (define (exponent e) (caddr e)) January 02 sicp 2.43(define (queens board-size)
(define (queen-cols k) (if (= k 0) (list empty-board) (filter (lambda (positions) (safe? k positions)) (flatmap (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row k rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1)))))) (queen-cols board-size)) (define empty-board (list))
(define (adjoin-position new-row k rest-of-queens) (cons (list new-row k) rest-of-queens)) (define (safe? k positions) (let ((new-row (car (car positions))) (rest-position (cdr positions))) (and (not (have-row? new-row rest-position)) (not (diagonal? new-row k rest-position)) ))) (define (have-row? new-row rest-position) (cond ((null? rest-position) #f) ((= new-row (car (car rest-position))) #t) (else (have-row? new-row (cdr rest-position))) )) (define (diagonal? new-row k rest-position) ;could split it into modules here (cond ((null? rest-position) #f)
((or (= (- new-row (car (car rest-position))) (- k (car (cdr (car rest-position))))) (= (- new-row (car (car rest-position))) (- (car (cdr (car rest-position))) k))) #t) (else (diagonal? new-row k (cdr rest-position))) )) ;code provided in the sicp textbook start here (define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence)) (define (enumerate-interval low high) (if (> low high) (list) (cons low (enumerate-interval (+ low 1) high)))) (define (remove item sequence) (filter (lambda (x) (not (= x item))) sequence)) (define (filter predicate sequence) (cond ((null? sequence) (list)) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) (define (flatmap proc seq) (accumulate append (list) (map proc seq))) (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) December 31 sicp 2.41(define (dist-order num seq)
(if (or (= num 0) (null? seq)) (list (list));very important indeed (flatmap (lambda (x) (map (lambda (i) (cons x i)) (dist-order (- num 1) (remove x seq)) )) seq))) (define (sum-triples-distinct sum high)
(filter (lambda (x) (= (fold-left + 0 x) sum)) (dist-order 3 (enumerate-interval 1 high)))) ;code provided in the sicp textbook start here
(define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence)) (define (enumerate-interval low high) (if (> low high) (list) (cons low (enumerate-interval (+ low 1) high)))) (define (remove item sequence) (filter (lambda (x) (not (= x item))) sequence)) (define (filter predicate sequence) (cond ((null? sequence) (list)) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) (define (flatmap proc seq) (accumulate append (list) (map proc seq))) (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) December 29 sicp 网上资源sicp主页 这个不用说,是学习sicp的第一站
sicp课程录像 sicp两位原著作者早期授课的录像
sicp交互式课程 这个是MIT公开课程和微软合作的icampus项目的一部分,提供课程的幻灯片,和与之对应的授课录音和脚本,和有交互式习题。值得说一下,其提供的习题和课本上的是不同的,因为这个比较新,其中有些授课内容书上是没有的。即可以做sicp课本的有效代替,也可以用来复习巩固学到的东西。
MIT电子工程和计算机科学公开课程 里面有课程介绍,阅读指南,课程中的project作业......
eli的sicp学习笔记 以色列网友提供的学习笔记和课本习题解答,解答用lisp写的,是不错的参考。这位网友的博客书评也很有看头。
December 27 sicp 2.36 2.37(define (dot-product v w)
(accumulate + 0 (map * v w))) (define (matrix-*-vector m v)
(map (lambda (x) (dot-product x v)) m)) (define (transpose mat)
(accumulate-n cons (list) mat)) (define (matrix-*-matrix m n)
(let ((cols (transpose n))) (map (lambda (x) (matrix-*-vector cols x)) m))) (define (accumulate-n op init seqs)
(if (null? (car seqs)) (list) (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs))))) (define (accumulate op initial sequence)
(if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) ;testing (define c (list (list 1 2 3 4) (list 4 5 6 6) (list 6 7 8 9) (list 1 10 1 10))) (define d (list 10 2 1 10)) (matrix-*-vector c d) (transpose c) (matrix-*-matrix c c) sicp 2.35;this is too damn hard
;I worked it out, and could hardly believe it worked, it works! (define (count-leaves t) (accumulate (lambda (x y) (+ x y)) 0 (map map-expand t))) (define (map-expand x) (if (not (pair? x)) 1 (count-leaves x))) (define (accumulate op initial sequence)
(if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) ;testing (define c (cons (list 1 2) (list 3 4))) (count-leaves c) (count-leaves (list c c)) sicp 2.34(define (horner-eval x coefficient-sequence) (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms))) 0 coefficient-sequence)) December 25 一美国网友的“中国威胁论”(有意思,我翻译了)在美国90%的维他命C产自中国。
美国只要稍稍惹毛中国,他们就会在那些维他命C里掺上些亚洲的流感病毒或者随便什么生化毒剂。
因为我们在很多食品中都添加维他命C,几乎所有的“活力”饮料,“果味”饮料,还有那些维他命C片,他们能轻而易举得消灭全美75%的人口。
同样得,他们可以在卖给宠物食品公司的添加剂里掺些料,要知道在美国几乎所有的宠物食品品牌都在用来自中国的添加剂,这样就可以杀死全美大多数宠物。值得注意的是,他们已经在这方面小试牛刀了,结果似乎很成功。
还记得在美国和别的国家造成橡树之死的中国蘑菇吗?美国情报部门相信那是中国特工有意在美国散播的噢!
还有那些被放到美国水系里的中国黑鱼---那些东西早已造成美国本土鱼数量锐减,恐怕同样和中国特工脱不了干系。
如果真的要把如此这些都列一张表,估计要写很长很长。
中国是一个实实在在的威胁。中国强大、睿智、又是唯利是图的拜金者。总得来说是百分百的邪恶存在!
---------
In the US, 90% of vitamin C (ascorbic acid) is now from China. All the US needs to do is piss China off a bit more and they will spike the vitamin C with a deadly Asian flu virus (or other bio-weapon).
As vitamin C is in many food products, almost all 'energy' drinks, 'fruit' drinks, vitamin pills themselves, etc., China could wipe out 75% of the US population this way.
Or that matter, they can easily kill many pets in American through spiking the ingredients they sell that are in virtually every brand of pet food. Oh wait, they already did the test run on that one. And it worked.
How about the death of oak trees in the US and other countries -- traced to a Chinese fungus that US intelligence believes was distributed throughout the US by Chinese agents.
Or Chinese fish introduced into US water systems that have decimated US fish populations. Same as above.
And the list goes on and on.
China is a true menace. 1B+ strong, smart, and totally dedicated to the pursuit of money above all other things. In other words, 100% evil. sicp 2.32;一个集合的所有子集可以被分为包括其中一元素q的子集集合alpha和不包括q的子集集合beta
;易见,在beta中的所有集合中添加元素q即得到alpha
(define (subsets s) (if (null? s) (list s) (let ((rest (subsets (cdr s)))) (append rest (map (lambda (x) (append (list (car s)) x)) rest))))) 自学者的福音(懒得翻译了,随便起个名)Some of the greatest people in history have educated themselves to a large degree using a process known as autodidacticism. This is something that's more easily undertaken these days with the great wealth of online tools available to anyone. Whether you've gone to college or not, you can learn just about anything these days on your own. Want to learn about the classics? Carpentry and home maintenance? Philosophy or cooking? Chess or computer programming? It's all online, and with a little bit of excitement, you can motivate yourself to learn a subject in a growing number of ways. Why self-education? Well, besides the obvious reasons of wanting to improve yourself, prepare yourself for success, and just learn as much as you can, self-education offers a few extra benefits: you can learn at your own pace, and in your own way. You can follow your passions, and learn about things that excite you. There's no price for failure, but there's every reward for success. How do you go about becoming an autodidact? The answer is simple: any way you want. I would suggest you set aside just a little time each day to learn a specific subject, but that really depends on your learning style. Some people learn all in one great rush: they'll stay up late hours for a few days in a row, consuming everything they possibly can about a subject. Others are overwhelmed by an approach like that, and would rather learn a little each day. However you go about it, here are some of the best tools for the modern autodidact:
December 24 自留地介绍听网友推荐,因为这本书在网上资料比较齐全,又都免费,所以就开始看。
在space上开地完全是因为我这人比较懒,为在公司家里实现资料共享而想出的笨办法。
如果哪位发现这些东西对你有用,那就算外部效用。
sicp的作业分为两部分。
一部分是书上的习题,帮助理解和扩展书的内容,还有一部分是作业,那是较贴近现实的规模稍大的应用。
这两部分我都计划完成。
所有习题如果没有特别注释,都是在drscheme环境下编辑并按R5RS运行通过的,个人认为这个东东比MIT提供的工具顺手。
sicp 2.29b(define (make-mobile left right)
(list left right)) (define (make-branch length structure) (list length structure)) (define (left-branch x) (car x)) (define (right-branch x) (car (cdr x))) (define (branch-length x) (car x)) (define (branch-structure x) (car (cdr x))) (define (total-weight x) (if (not (pair? x)) ;x is number? x (+ (total-weight (branch-structure (left-branch x))) (total-weight (branch-structure (right-branch x)))))) ;for testing (total-weight (list (list 7 (list (list 3 5) (list 4 6))) (list 1 1))) |
|
|