Merge pull request #238 from prt2121/pt/haskell-7.10.1
[jackhill/mal.git] / tests / step4_if_fn_do.mal
1 ;; -----------------------------------------------------
2
3
4 ;; Testing list functions
5 (list)
6 ;=>()
7 (list? (list))
8 ;=>true
9 (empty? (list))
10 ;=>true
11 (empty? (list 1))
12 ;=>false
13 (list 1 2 3)
14 ;=>(1 2 3)
15 (count (list 1 2 3))
16 ;=>3
17 (count (list))
18 ;=>0
19 (count nil)
20 ;=>0
21 (if (> (count (list 1 2 3)) 3) "yes" "no")
22 ;=>"no"
23 (if (>= (count (list 1 2 3)) 3) "yes" "no")
24 ;=>"yes"
25
26
27 ;; Testing if form
28 (if true 7 8)
29 ;=>7
30 (if false 7 8)
31 ;=>8
32 (if true (+ 1 7) (+ 1 8))
33 ;=>8
34 (if false (+ 1 7) (+ 1 8))
35 ;=>9
36 (if nil 7 8)
37 ;=>8
38 (if 0 7 8)
39 ;=>7
40 (if "" 7 8)
41 ;=>7
42 (if (list) 7 8)
43 ;=>7
44 (if (list 1 2 3) 7 8)
45 ;=>7
46 (= (list) nil)
47 ;=>false
48
49
50 ;; Testing 1-way if form
51 (if false (+ 1 7))
52 ;=>nil
53 (if nil 8 7)
54 ;=>7
55 (if true (+ 1 7))
56 ;=>8
57
58
59 ;; Testing basic conditionals
60 (= 2 1)
61 ;=>false
62 (= 1 1)
63 ;=>true
64 (= 1 2)
65 ;=>false
66 (= 1 (+ 1 1))
67 ;=>false
68 (= 2 (+ 1 1))
69 ;=>true
70 (= nil 1)
71 ;=>false
72 (= nil nil)
73 ;=>true
74
75 (> 2 1)
76 ;=>true
77 (> 1 1)
78 ;=>false
79 (> 1 2)
80 ;=>false
81
82 (>= 2 1)
83 ;=>true
84 (>= 1 1)
85 ;=>true
86 (>= 1 2)
87 ;=>false
88
89 (< 2 1)
90 ;=>false
91 (< 1 1)
92 ;=>false
93 (< 1 2)
94 ;=>true
95
96 (<= 2 1)
97 ;=>false
98 (<= 1 1)
99 ;=>true
100 (<= 1 2)
101 ;=>true
102
103
104 ;; Testing equality
105 (= 1 1)
106 ;=>true
107 (= 0 0)
108 ;=>true
109 (= 1 0)
110 ;=>false
111 (= "" "")
112 ;=>true
113 (= "abc" "abc")
114 ;=>true
115 (= "abc" "")
116 ;=>false
117 (= "" "abc")
118 ;=>false
119 (= "abc" "def")
120 ;=>false
121 (= "abc" "ABC")
122 ;=>false
123
124 (= (list) (list))
125 ;=>true
126 (= (list 1 2) (list 1 2))
127 ;=>true
128 (= (list 1) (list))
129 ;=>false
130 (= (list) (list 1))
131 ;=>false
132 (= 0 (list))
133 ;=>false
134 (= (list) 0)
135 ;=>false
136 (= (list) "")
137 ;=>false
138 (= "" (list))
139 ;=>false
140
141
142 ;; Testing builtin and user defined functions
143 (+ 1 2)
144 ;=>3
145 ( (fn* (a b) (+ b a)) 3 4)
146 ;=>7
147 ( (fn* () 4) )
148 ;=>4
149
150 ( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
151 ;=>8
152
153
154 ;; Testing closures
155 ( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
156 ;=>12
157
158 (def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))
159 (def! plus5 (gen-plus5))
160 (plus5 7)
161 ;=>12
162
163 (def! gen-plusX (fn* (x) (fn* (b) (+ x b))))
164 (def! plus7 (gen-plusX 7))
165 (plus7 8)
166 ;=>15
167
168 ;; Testing do form
169 (do (prn "prn output1"))
170 ; "prn output1"
171 ;=>nil
172 (do (prn "prn output2") 7)
173 ; "prn output2"
174 ;=>7
175 (do (prn "prn output1") (prn "prn output2") (+ 1 2))
176 ; "prn output1"
177 ; "prn output2"
178 ;=>3
179
180 (do (def! a 6) 7 (+ a 8))
181 ;=>14
182 a
183 ;=>6
184
185 ;; Testing special form case-sensitivity
186 (def! DO (fn* (a) 7))
187 (DO 3)
188 ;=>7
189
190 ;; Testing recursive sumdown function
191 (def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
192 (sumdown 1)
193 ;=>1
194 (sumdown 2)
195 ;=>3
196 (sumdown 6)
197 ;=>21
198
199
200 ;; Testing recursive fibonacci function
201 (def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
202 (fib 1)
203 ;=>1
204 (fib 2)
205 ;=>2
206 (fib 4)
207 ;=>5
208 ;;; Too slow for bash, erlang, make and miniMAL
209 ;;;(fib 10)
210 ;;;;=>89
211
212
213 ;>>> deferrable=True
214 ;;
215 ;; -------- Deferrable Functionality --------
216
217 ;; Testing variable length arguments
218
219 ( (fn* (& more) (count more)) 1 2 3)
220 ;=>3
221 ( (fn* (& more) (list? more)) 1 2 3)
222 ;=>true
223 ( (fn* (& more) (count more)) 1)
224 ;=>1
225 ( (fn* (& more) (count more)) )
226 ;=>0
227 ( (fn* (& more) (list? more)) )
228 ;=>true
229 ( (fn* (a & more) (count more)) 1 2 3)
230 ;=>2
231 ( (fn* (a & more) (count more)) 1)
232 ;=>0
233 ( (fn* (a & more) (list? more)) 1)
234 ;=>true
235
236
237 ;; Testing language defined not function
238 (not false)
239 ;=>true
240 (not true)
241 ;=>false
242 (not "a")
243 ;=>false
244 (not 0)
245 ;=>false
246
247
248 ;; -----------------------------------------------------
249
250 ;; Testing string quoting
251
252 ""
253 ;=>""
254
255 "abc"
256 ;=>"abc"
257
258 "abc def"
259 ;=>"abc def"
260
261 "\""
262 ;=>"\""
263
264 "abc\ndef\nghi"
265 ;=>"abc\ndef\nghi"
266
267 "abc\\def\\ghi"
268 ;=>"abc\\def\\ghi"
269
270 ;; Testing pr-str
271
272 (pr-str)
273 ;=>""
274
275 (pr-str "")
276 ;=>"\"\""
277
278 (pr-str "abc")
279 ;=>"\"abc\""
280
281 (pr-str "abc def" "ghi jkl")
282 ;=>"\"abc def\" \"ghi jkl\""
283
284 (pr-str "\"")
285 ;=>"\"\\\"\""
286
287 (pr-str (list 1 2 "abc" "\"") "def")
288 ;=>"(1 2 \"abc\" \"\\\"\") \"def\""
289
290 (pr-str "abc\ndef\nghi")
291 ;=>"\"abc\\ndef\\nghi\""
292
293 (pr-str "abc\\def\\ghi")
294 ;=>"\"abc\\\\def\\\\ghi\""
295
296 (pr-str (list))
297 ;=>"()"
298
299 ;; Testing str
300
301 (str)
302 ;=>""
303
304 (str "")
305 ;=>""
306
307 (str "abc")
308 ;=>"abc"
309
310 (str "\"")
311 ;=>"\""
312
313 (str 1 "abc" 3)
314 ;=>"1abc3"
315
316 (str "abc def" "ghi jkl")
317 ;=>"abc defghi jkl"
318
319 (str "abc\ndef\nghi")
320 ;=>"abc\ndef\nghi"
321
322 (str "abc\\def\\ghi")
323 ;=>"abc\\def\\ghi"
324
325 (str (list 1 2 "abc" "\"") "def")
326 ;=>"(1 2 abc \")def"
327
328 (str (list))
329 ;=>"()"
330
331 ;; Testing prn
332 (prn)
333 ;
334 ;=>nil
335
336 (prn "")
337 ; ""
338 ;=>nil
339
340 (prn "abc")
341 ; "abc"
342 ;=>nil
343
344 (prn "abc def" "ghi jkl")
345 ; "abc def" "ghi jkl"
346
347 (prn "\"")
348 ; "\""
349 ;=>nil
350
351 (prn "abc\ndef\nghi")
352 ; "abc\ndef\nghi"
353 ;=>nil
354
355 (prn "abc\\def\\ghi")
356 ; "abc\\def\\ghi"
357 nil
358
359 (prn (list 1 2 "abc" "\"") "def")
360 ; (1 2 "abc" "\"") "def"
361 ;=>nil
362
363
364 ;; Testing println
365 (println)
366 ;
367 ;=>nil
368
369 (println "")
370 ;
371 ;=>nil
372
373 (println "abc")
374 ; abc
375 ;=>nil
376
377 (println "abc def" "ghi jkl")
378 ; abc def ghi jkl
379
380 (println "\"")
381 ; "
382 ;=>nil
383
384 (println "abc\ndef\nghi")
385 ; abc
386 ; def
387 ; ghi
388 ;=>nil
389
390 (println "abc\\def\\ghi")
391 ; abc\def\ghi
392 ;=>nil
393
394 (println (list 1 2 "abc" "\"") "def")
395 ; (1 2 abc ") def
396 ;=>nil
397
398 ;>>> optional=True
399 ;;
400 ;; -------- Optional Functionality --------
401
402 ;; Testing keywords
403 (= :abc :abc)
404 ;=>true
405 (= :abc :def)
406 ;=>false
407 (= :abc ":abc")
408 ;=>false
409
410 ;; Testing vector truthiness
411 (if [] 7 8)
412 ;=>7
413
414 ;; Testing vector printing
415 (pr-str [1 2 "abc" "\""] "def")
416 ;=>"[1 2 \"abc\" \"\\\"\"] \"def\""
417
418 (pr-str [])
419 ;=>"[]"
420
421 (str [1 2 "abc" "\""] "def")
422 ;=>"[1 2 abc \"]def"
423
424 (str [])
425 ;=>"[]"
426
427
428 ;; Testing vector functions
429 (count [1 2 3])
430 ;=>3
431 (empty? [1 2 3])
432 ;=>false
433 (empty? [])
434 ;=>true
435 (list? [4 5 6])
436 ;=>false
437
438 ;; Testing vector equality
439 (= [] (list))
440 ;=>true
441 (= [7 8] [7 8])
442 ;=>true
443 (= (list 1 2) [1 2])
444 ;=>true
445 (= (list 1) [])
446 ;=>false
447 (= [] [1])
448 ;=>false
449 (= 0 [])
450 ;=>false
451 (= [] 0)
452 ;=>false
453 (= [] "")
454 ;=>false
455 (= "" [])
456 ;=>false
457
458 ;; Testing vector parameter lists
459 ( (fn* [] 4) )
460 ;=>4
461 ( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
462 ;=>8
463
464 ;; Nested vector/list equality
465 (= [(list)] (list []))
466 ;=>true
467 (= [1 2 (list 3 4 [5 6])] (list 1 2 [3 4 (list 5 6)]))
468 ;=>true