-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheme
More file actions
201 lines (194 loc) · 6.21 KB
/
Scheme
File metadata and controls
201 lines (194 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Racket
http://racket-lang.org/
#lang racket
(provide (all-defined-out))
(define x 3)
(define y (+ x 2))
(define cube1
(lambda (x)
(* x ( * x x))))
(define cube2
(lambda (x)
(* x x )))
(define (cube3 x)
(* x x x))
> (+ 2 3)
5
> (+ (* 3(+ (* 2 4)(+ 3 5)))(+(- 10 7) 6))
57
======================================================================================
> (define size 2)
> size
2
> (* size size)
4
======================================================================================
> (define (square x)(* x x)) #最直接的子函数
> (square 10)
100
> (define (sum-squares x y)(+ (square x) (square y))) #嵌套过一层
> (sum-squares 9 1)
82
> (define (f a) (sum-squares (+ a 1) (* a 2))) #嵌套两层,f的参数a +1和*2之
> (f 5) 后传给sum-squares函数再继续传
136
======================================================================================
> (define (abs x)
(cond ((< x 0) (- x))
(else x)))
> (abs -10)
10
> (define (abs2 x)
(if(< x 0)(- x)
x))
> (abs2 -10)
10
> (define (>= x y) #定义了一个新的谓语‘>=’
(or (> x y)(= x y))) 就是(x>y)or (x==y)
> (>= 10 10)
#t
> (>= 9 10)
#f
======================================================================================
> (define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter(improve guess x) x)))
(define (average x y)(/ (+ x y) 2))
(sqrt-iter 1.0 x))
> (sqrt 100)
10.000000000139897
======================================================================================
(define (gcd a b)
(if (= b 0) a
(gcd b (remainder a b))))
======================================================================================
> (define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
> (factorial 6)
720
> (define (factorial2 n)
(define (re pro acc n)
(if (> acc n)
pro
(re (* pro acc) (+ 1 acc) n)))
(re 1 1 n)
)
> (factorial2 6)
720
======================================================================================
>(define (fib n)
(define (f a b acc n)
(if (= acc n)
b
(f (+ a b) a (+ acc 1) n)))
(f 0 1 0 n))
>(fib 5)
3
======================================================================================
(define (T m n)
(cond((= m 0) 1)
((or(< m 0)(= n 0)) 0)
(else (+ (T m (- n 1)) (T (- m (first-coin n))n)))))
(define (first-coin kindsofn)
(cond ((= kindsofn 1) 1)
((= kindsofn 2) 5)
((= kindsofn 3) 10)
((= kindsofn 4) 25)
((= kindsofn 5) 50)))
########################## T(m,n)=T(m,n-1)+T(m-a,n) ##################################
########################## T(0,n)=1,T(m<0,n)=T(m,0)=0 ################################
======================================================================================
(define (prime? n)
(define (smallest-divisor n) (find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (* test-divisor 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))
(if (= (smallest-divisor n) n)(display "yes")(display"no")))
###################################### 简单素性检测 ##################################
======================================================================================
(define (add-rat x y)
(make-rat (+(*(numer x)(denom y))
(*(denom x)(numer y)))
(* (denom x)(denom y))))
(define (sub-rat x y)
(make-rat (-(*(numer x)(denom y))
(*(denom x)(numer y)))
(* (denom x)(denom y))))
(define (mul-rat x y)
(make-rat (*(numer x)(numer y))
(*(denom y)(denom x))))
(define (div-rat x y)
(make-rat (*(numer x)(denom y))
(*(denom x)(numer y))))
(define (equal-rat? x y)
(= (*(numer x)(denom y))(*(denom x)(numer y))))
(define (make-rat n d)(cons n d))
(define (numer x)(car x))
(define (denom x)(cdr x))
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x)))
======================================================================================
(define a (list 1 2 3 4)) //定义a是一个列表
(car a)
》1
(cdr a)
》'(2 3 4)
(cons 6 a) //a并没有改变
(define b (cons 5 a)) //b=(5,1,2,3,4),a=(1,2,3,4)
======================================================================================
(define a (list 1 2 3 4 5 6 7))
(define (list-ref list n) //取元素函数,如果要取list中的第k位,n取
(if (= n 0) (car list) //(k-1)
(list-ref (cdr list) (- n 1))))
(define (length list) //求长度函数
(if (null? list)
0
(+ 1 (length (cdr list)))))
(define (len list) //求长度函数(迭代版本)
(define (f list n)
(if (null? list)
n
(f (cdr list) (+ n 1))))
(f list 0)
)
(define (append a b) //把b贴到a后面,实际上,a和b都没变
(if (null? a)
b
(cons (car a)(append (cdr a) b))))
(define (last-pair list) //返回最后一个元素
(if (null? (cdr list))
(car list)
(last-pair (cdr list))))
(define (count-leaves x)
(cond ((null? x)0)
((not (pair? x))1)
(else (+ (count-leaves (car x))(count-leaves (cdr x))))))
(define (reverse list)
(define (f nu li)
(if (null? li)
nu
(f (cons (car li) nu) (cdr li))))
(f null list))
======================================================================================
(define (map pro list)
(if (null? list)
null
(cons (pro (car list))
(map pro (cdr list)))))
======================================================================================
(lambda (x y) (+ x y))
//just think as an op or a function without name