-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.chl
271 lines (241 loc) · 5.47 KB
/
run.chl
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
;; Test 01
(println "----------1----------")
(let a "Hello ")
(let b "World!")
(println (+ a b)) ; Hello World!
;; Test 02
(println "----------2----------")
(let c [9 8 7 6 5 4 3 2 1 0])
(println c) ; [9 8 7 6 5 4 3 2 1 0]
(sort c)
(println c) ; [0 1 2 3 4 5 6 7 8 9]
;; Test 03
(println "----------3----------")
(let d [9/5 8/8 7/8 67/2 5/10 4/1 3/2 2/5 1/7 0/5])
(println d) ; [9/5 1 7/8 67/2 1/2 4 3/2 2/5 1/7 0]
(sort d)
(println d) ; [0 1/7 2/5 1/2 7/8 1 3/2 9/5 4 67/2]
;; Test 04
(println "----------4----------")
(let e [5 1 3])
(let f e)
(cpy g e)
(sort e)
(println f) ; [1 3 5]
(println g) ; [5 1 3]
(let e [5 1 3])
(println e) ; [5 1 3]
(println (sort (cpy e))) ; [1 3 5]
(println e) ; [5 1 3]
;; Test 05
(println "----------5----------")
(let e (list 1 2 3))
(println e) ; [1 2 3]
(println (disp e)) ; 123
;; Test 06
(println "----------6----------")
(let a 5)
(let b 6)
(let c [a b])
(p (+ (disp c))) ; 11
(let d [+ 5 6])
(p ((disp d))) ; 11
;(let e (disp [+ 5 6])) ; Syntax error
;; Test 07
(println "----------7----------")
(let add +)
(println add) ; F(+)
(println (add 5 6)) ; 11
(let a [9 5 7])
(println (sort (cpy a))) ; [5 7 9]
(println (sort (cpy a) <)) ; [5 7 9]
(println (sort (cpy a) >)) ; [9 7 5]
(println (sort (cpy a) +)) ; Error
(let small <)
(println (sort (cpy a) small)) ; [5 7 9]
;; Test 08
(println "----------8----------")
(println (+ 5 6)) ; 11
(println ((disp [+]) 5 6)) ; 11
(println ((disp [+ 5 6]))) ; 11
(println (call + 5 6)) ; 11
(println (call (disp [+]) 5 6)) ; 11
(println (call (disp [+ 5 6]))) ; 11
;; Test 09
(println "----------9----------")
(let a 5)
(ref b a)
(println a "," b) ; 5,5
(let a 6)
(println a "," b) ; 6,6
;; Test 10
;;(println "----------10----------")
;;(p (let a 5)) ; Null ; old : a(5)
;;(p (cpy b 5)) ; Null ; old : b(5)
;;(p (ref c 5)) ; Null ; old : c(5)
;;(p (let d a)) ; Null ; old : d(5)
;;(p (cpy e a)) ; Null ; old : e(5)
;;(p (ref f a)) ; Null ; old : f(a(5))
;;(p (let g f)) ; Null ; old : g(a(5))
;;(p (cpy h f)) ; Null ; old : h(5)
;;(p (ref i f)) ; Null ; old : i(a(5))
;; Test 11
(println "----------11----------")
(let a 5) (p a) ; a(5)
(ref b a) (p b) ; b(a(5))
(ref a b) (p a) ; a(5)
(ref a a) (p a) ; a(5)
;; Test 12
(println "----------12----------")
(let a (list 9 7 6 3 1))
(let b (list 2 4 5 8 0))
(println a) ; [9 7 6 3 1]
(println b) ; [2 4 5 8 0]
(p (+ a b)) ; [9 7 6 3 1 2 4 5 8 0]
(p (sort (+ a b) <)) ; [0 1 2 3 4 5 6 7 8 9]
(p (foreach + a b)) ; [11 11 11 11 1]
(p (foreach list a b)) ; [[9 2] [7 4] [6 5] [3 8] [1 0]]
;; Test 13
(println "----------13----------")
(let a [9 8 7 6 5 4 3 2 1 0])
(let b (+ a a a a a a a a a a))
(Config.SetPrintIntervalTime T)
(println "sort:")
(sort (cpy b))
(println "sort <:")
(sort (cpy b) <)
(println "sort >:")
(sort (cpy b) >)
(Config.SetPrintIntervalTime F)
;; Test 14
(println "----------14----------")
(Config.SetPrintIntervalTime T)
(let a (* [9 8 7 6 5 4 3 2 1 0] 1000))
(p (size (foreach + a)))
(p (size (sort a <)))
(Config.SetPrintIntervalTime F)
;; Test 15
(println "----------15----------")
(p (? T 5 4)) ; 5
(p (? F 5)) ; nil
(p (if T 5 else 4)) ; 5
(p (if F 5)) ; nil
(let a T)
(if a then ; I'm T
(println "I'm T")
else
(println "I'm F")
)
(p ((if T + else -) 5 6)) ; 11
(p ((if F + else -) 5 6)) ; -1
(let x 5)
(if (= x 5) then
(let x 6)
)
(p x) ; x(6)
;; Test 16
(println "----------16----------")
(let j 1)
(loop
(let i 1)
(loop
(print i "*" j "=" (* i j) " ")
(if (= i j) break)
(let i (inc i))
)
(println)
(if (= j 9) break)
(let j (inc j))
)
;; Test 17
(println "----------17----------")
(let j 1)
(while (<= j 9)
(let i 1)
(while (<= i j)
(print i "*" j "=" (* i j) " ")
(let i (inc i))
)
(println)
(let j (inc j))
)
;; Test 18
(println "----------18----------")
(for j in 1 to 9
(for i in 1 to j
(print i "*" j "=" (* i j) " ")
)
(println)
)
;; Test 19
(println "----------19----------")
(let array [9 8 7 6 5 4 3 2 1])
(let n (- (size array) 1))
(for j in 0 to (- n 1)
(for i in 0 to (- n 1 j)
(let t (+ i 1))
(if (> (at array i) (at array t))
(swap array i t)
)
)
)
(println array)
;; Test 20
(println "----------20----------")
(let a [])
(let b 5)
(push a b)
(let b 50)
(push a b)
(println a) ; [5 50]
;; Test 21
(println "----------21----------")
(let arr [2 3])
(let count 5)
(Config.SetPrintIntervalTime T)
(while (< count 100)
(let b T)
(for i in 1 to (- (size arr) 1)
(if (= (mod count (at arr i)) 0)
(let b F)
break
)
)
(if b (push arr count))
(let count (+ count 2))
)
(Config.SetPrintIntervalTime F)
(println arr)
;; Test 22
(println "----------22----------")
(p (type 5)) ; Number
(let a 5) (p (type a)) ; Number
(ref a 5) (p (type a)) ; Number
;; Test 23
(println "----------23----------")
(restrict str String)
(let str "String")
(p str) ; str("String")
(let str 6) ; Error
(p str) ; str("String")
;; Test 24
(println "----------24----------")
(let a [])
(for i in 0 to 10 (push a (rand 100)))
(println a)
;; Test 25
(println "----------25----------")
; define
; (define x 6)
; (define y x)
; (define z (+ x y))
; (define m (f x y z))
(module MathTest
(define add +)
(define sub -)
)
(p (MathTest.add 5 6)) ; 11
(module MathTest2
(import MathTest) ; Name finding starts from Global
(define add2 add)
)