;递归版本 (define (repeated f n) (if (= n 1) f (lambda (x) (f ((repeated f (- n 1)) x))))) ;迭代版本 (define (re-iter f n) (define (iter i f-result) (if (= i n) f-result (iter (+ i 1) (lambda (x) (f (f-result x)))))) (iter 1 f))
根据提示,可以使用EX 1.42的compose过程,省去了使用lambda
1 2 3 4 5 6 7 8 9 10 11 12
;使用compose的递归版本 (define (repeated f n) (if (= n 1) f (compose f (repeated f (- n 1))))) ;使用compose的迭代版本 (define (repeated f n) (define (iter i f-result) (if (= i n) f-result (iter (+ i 1) (compose f f-result)))) (iter 1 f))
;accumulate递归版本 (define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b)))) ;sum测试,计算立方和 (define (cube-sum a b) (accumulate + 0 cube a next b)) ;product测试,计算阶乘 (define (factorial n) (define (f x) x) (accumulate * 1 f 1 next n))
(cube-sum 1 10)
(factorial 6)
结果
b)
1 2 3 4 5 6 7
;accumulate迭代版本 (define (accumulate combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (combiner (term a) result)))) (iter a null-value))
EX 1.33
a)
1 2 3 4 5 6 7 8 9 10 11 12
;相比EX 1.32,增加参数filter,用于过滤一些项 (define (filtered-accumulate combiner null-value filter f a next b) (if (> a b) null-value (if (filter a) (combiner (f a) (filtered-accumulate combiner null-value filter f (next a) next b)) (filtered-accumulate combiner null-value filter f (next a) next b)))) ;计算a到b之间素数和 (define (prime-sum a b) (define (f x) x) (filtered-accumulate + 0 prime? f a next b)) ;prime?见书P33页
其实可以使用let来定义剩余项,因为按照书中课后习题的学习顺序,这里并没有使用let
(let rest (filtered-accumulate combiner null-value filter f (next a) next b))
b)
1 2 3 4
(define (gcd-product n) (define (f x) x) (define (its-prime? x) (= (gcd x n) 1)) (filtered-accumulate * 1 its-prime? f 1 next n))
;38页递归版本sum (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (simpson f a b n) (define h (/ (- b a) n)) (define (yk k) (f (+ a (* k h)))) (define (next k) (+ k 1)) (define (coefficient k) (cond ((or (= k 0) (= k n)) 1) ((odd? k) 4) (else 2))) (define (term k) (* (coefficient k) (yk k))) (* (sum term 0 next n) (/ h 3)))
结果
EX 1.30
1 2 3 4 5 6 7
;sum的迭代版本 (define (sum term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ (term a) result)))) (iter a 0))
;类似于sum的过程product,递归版本 (define (product f a next b) (if (> a b) 1 (* (f a) (product f (next a) next b)))) ;用product定义阶乘过程factorial (define (factorial n) (define (f x) x) (define (next i) (+ i 1)) (product f 1 next n)) ;计算pi值,首先计算每一个分数 (define (fraction i) (if (odd? i) (/ (+ i 1) (+ i 2)) (/ (+ i 2) (+ i 1)))) ;计算pi值,n是精确度 (define (pi n) (define (next i) (+ i 1)) (* (product fraction 1.0 next n) 4.0))
结果
b)
1 2 3 4 5 6 7
;product的迭代版本 (define (product f a next b) (define (iter a result) (if (> a b) result (iter (next a) (* (f a) result)))) (iter a 1))
Play is an open source web application framework, written in Scala and Java, which follows the model–view–controller (mvc) architectural pattern. It aims to optimize developer productivity by using convention over configuration, hot code reloading and display of errors in the browser. Support for the Scala programming language has been available since version 1.1 of the framework.In version 2.0, the framework core was rewritten in Scala. Build and deployment was migrated to SBT, and templates use Scala instead of Groovy.
约定优于配置(convention over configuration),也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。 本质是说,开发人员仅需规定应用中不符约定的部分。例如,如果模型中有个名为Sale的类,那么数据库中对应的表就会默认命名为sales。只有在偏离这一约定时,例如将该表命名为”products_sold”,才需写有关这个名字的配置。
Typesafe (now Lightbend) is a company founded by Martin Odersky, the creator of the Scala programming language, Jonas Bonér, the creator of the Akka middleware, and Paul Phillips in 2011. It provides an Open source platform for building Reactive applications for the JVM, consisting of the Play Framework, Akka middleware and Scala programming language—with additional supporting products and development tools such as the Scala IDE for Eclipse, the Slick database query and access library for Scala and the sbt build tool. Typesafe also provides training, consulting and commercial support on the platform. Typesafe is one of the main contributors of Reactive Streams.
这里有几个点值得关注
Akka: 简化并发问题的开源工具包
Reactive: Reactive Programming, 一种面向数据流和变化传播的编程范式
sbt: 构建Scala和Java项目的工具,类似于Maven和Ant
言归正传,Play的工程结构这样的,MVC层次分明。
在一开始配置play2.2的时候,遇到的主要的问题有
集成在Intellij 13中
依赖的jar包很多
我采用的办法是使用Activator直接new play application,再导入Intellij中。所依赖的jar包很多,只能从公司的服务器上把ivy仓库全部download下来覆盖本地。 后来用了intellij 14、15版本,对于Scala、playframework的集成好多了,直接装个Scala plugin,ivy一覆盖就行。
(define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0))
(smallest-divisor 199) ;answer is 199
(smallest-divisor 1999) ;answer is 1999
(smallest-divisor 19999) ;answer is 7
;生成奇数 (define (odd-generator x) (if (odd? x) (+ x 2) (+ x 1)))
1 2 3 4 5 6 7 8
;大于x的前i个素数 (define (continue-prime i x) (cond ((= i 0) (display "These are all primes")) ((prime? x) (display x) (newline) (continue-prime (- i 1) (odd-generator x))) (else (continue-prime i (odd-generator x)))))
1 2 3 4 5 6 7 8 9 10 11
;判断素数,可以采用33页最基本的方法 (define (prime? n) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) (= n (smallest-divisor n)))
(define (new-prime? n) (define (next x) (if (= x 2) 3 (+ x 2))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (next test-divisor))))) (define (divides? a b) (= (remainder b a) 0)) (= n (smallest-divisor n)))
然后用这个新的素数判断过程重写continue-prime
1 2 3 4 5 6 7
(define (continue-newprime i x) (cond ((= i 0) (display "These are all primes")) ((new-prime? x) (display x) (newline) (continue-prime (- i 1) (odd-generator x))) (else (continue-prime i (odd-generator x)))))
time = O(a) space = O(a) 对于过程2,显然空间复杂度会是一个常数(当然这也并不完全准确,随着计算的数增大,所需要的空间也在变化) time = O(a) space = O(1)
EX 1.10
(A 1 10) ;answer is 1024
(A 2 4) ;answer is 65536
(A 3 3) ;answer is 65536
(define (f n) (A 0 n)) ;f(n)=2n
(define (g n) (A 1 n)) ;g(n)=2^n
(define (k n) (A 2 n)) ;k(n)=2^2^2^…… 连续求n次平方
EX 1.11
递归版本
(define (f n)
(if (< n 3)
n
(+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))
迭代版本
(define (f n)
(define (iter n a b c i)
(if (= i n)
c
(iter n
(+ a (* 2 b) (* 3 c))
a
b
(+ i 1))))
(iter n 2 1 0 0))
(define (even? x) (= (remainder x 2) 0)) (define (square x) (* x x)) (define (exp b n) (define (exp-iter b n p) (cond ((= n 0) p) ((even? n) (exp-iter (square b) (/ n 2) p)) (else (exp-iter b (- n 1) (* b p))))) (exp-iter b n 1))
EX 1.17
1 2 3 4 5 6 7 8 9 10
(define (double x) (+ x x)) (define (halve x) (/ x 2)) (define (even? x) (= (remainder x 2) 0)) (define (expt b n) (cond ((= n 0) 0) ((even? n) (double (expt b (halve n)))) (else (+ b (expt b (- n 1))))))
EX 1.18
1 2 3 4 5 6 7 8 9 10 11 12
(define (double x) (+ x x)) (define (halve x) (/ x 2)) (define (even? x) (= (remainder x 2) 0)) (define (multi a b) (define (multi-iter a b p) (cond ((= b 0) p) ((even? b) (multi-iter (double a) (halve b) p)) (else (multi-iter a (- b 1) (+ a p))))) (multi-iter a b 0))
EX 1.19
通过两次变换,可以得到 p’ = p^2 + q^2 q’ = 2pq + q^2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(define (even? x) (= (remainder x 2) 0)) (define (fib n) (define (fib-iter a b p q count) (cond ((= count 0) b) ((even? count) (fib-iter a b (+ (square p) (square q)) (+ (* 2 p q) (square q)) (/ count 2))) (else (fib-iter (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) p q (- count 1))))) (fib-iter 1 0 0 1 n))
而以上三点,也对应了书中前四章的内容,在控制复杂度的过程中,必然涉及到程序架构的问题,作者也给出了两个方向,operations on aggregates (stream)——聚集操作,和著名的object-oriented——面向对象。也许是因为MIT的CS专业是和EE紧密相连的,各种电气系统的概念总能看到。