-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.scm
684 lines (524 loc) · 11.9 KB
/
tests.scm
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
;;; Test cases for Scheme.
;;;
;;; In order to run only a prefix of these examples, add the line
;;;
;;; (exit)
;;;
;;; after the last test you wish to run.
;;; ********************************
;;; *** Add your own tests here! (Optional) ***
;;; ********************************
;;; These are examples from several sections of "The Structure
;;; and Interpretation of Computer Programs" by Abelson and Sussman.
;;; License: Creative Commons share alike with attribution
;;; 1.1.1
10
; expect 10
(+ 137 349)
; expect 486
(- 1000 334)
; expect 666
(* 5 99)
; expect 495
(/ 10 5)
; expect 2
(+ 2.7 10)
; expect 12.7
(+ 21 35 12 7)
; expect 75
(* 25 4 12)
; expect 1200
(+ (* 3 5) (- 10 6))
; expect 19
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
; expect 57
(+ (* 3
(+ (* 2 4)
(+ 3 5)))
(+ (- 10 7)
6))
; expect 57
;;; 1.1.2
(define size 2)
; expect size
size
; expect 2
(* 5 size)
; expect 10
(define pi 3.14159)
(define radius 10)
(* pi (* radius radius))
; expect 314.159
(define circumference (* 2 pi radius))
circumference
; expect 62.8318
;;; 1.1.4
(define (square x) (* x x))
; expect square
(square 21)
; expect 441
(define square (lambda (x) (* x x))) ; See Section 1.3.2
(square 21)
; expect 441
(square (+ 2 5))
; expect 49
(square (square 3))
; expect 81
(define (sum-of-squares x y)
(+ (square x) (square y)))
(sum-of-squares 3 4)
; expect 25
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
(f 5)
; expect 136
;;; 1.1.6
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
(abs -3)
; expect 3
(abs 0)
; expect 0
(abs 3)
; expect 3
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
(a-plus-abs-b 3 -2)
; expect 5
;;; 1.1.7
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt 9)
; expect 3.00009155413138
(sqrt (+ 100 37))
; expect 11.704699917758145
(sqrt (+ (sqrt 2) (sqrt 3)))
; expect 1.7739279023207892
(square (sqrt 1000))
; expect 1000.000369924366
;;; 1.1.8
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))
(sqrt 9)
; expect 3.00009155413138
(sqrt (+ 100 37))
; expect 11.704699917758145
(sqrt (+ (sqrt 2) (sqrt 3)))
; expect 1.7739279023207892
(square (sqrt 1000))
; expect 1000.000369924366
;;; 1.3.1
(define (cube x) (* x x x))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum cube a inc b))
(sum-cubes 1 10)
; expect 3025
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
(sum-integers 1 10)
; expect 55
;;; 1.3.2
((lambda (x y z) (+ x y (square z))) 1 2 3)
; expect 12
(define (f x y)
(let ((a (+ 1 (* x y)))
(b (- 1 y)))
(+ (* x (square a))
(* y b)
(* a b))))
(f 3 4)
; expect 456
(define x 5)
(+ (let ((x 3))
(+ x (* x 10)))
x)
; expect 38
(let ((x 3)
(y (+ x 2)))
(* x y))
; expect 21
;;; 2.1.1
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(define (equal-rat? x y)
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
(define x (cons 1 (cons 2 nil)))
(car x)
; expect 1
(cdr x)
; expect (2)
(define x (list 1 2))
(define y (list 3 4))
(define z (cons x y))
(car (car z))
; expect 1
(car (cdr z))
; expect 3
z
; expect ((1 2) 3 4)
(define (make-rat n d) (list n d))
(define (numer x) (car x))
(define (denom x) (car (cdr x)))
(define (print-rat x)
(display (numer x))
(display '/)
(display (denom x))
(newline))
(define one-half (make-rat 1 2))
(print-rat one-half)
; expect 1/2
(define one-third (make-rat 1 3))
(print-rat (add-rat one-half one-third))
; expect 5/6
(print-rat (mul-rat one-half one-third))
; expect 1/6
(print-rat (add-rat one-third one-third))
; expect 6/9
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
(define (make-rat n d)
(let ((g (gcd n d)))
(list (/ n g) (/ d g))))
(print-rat (add-rat one-third one-third))
; expect 2/3
(define one-through-four (list 1 2 3 4))
one-through-four
; expect (1 2 3 4)
(car one-through-four)
; expect 1
(cdr one-through-four)
; expect (2 3 4)
(car (cdr one-through-four))
; expect 2
(cons 10 one-through-four)
; expect (10 1 2 3 4)
(cons 5 one-through-four)
; expect (5 1 2 3 4)
(define (map proc items)
(if (null? items)
nil
(cons (proc (car items))
(map proc (cdr items)))))
(map abs (list -10 2.5 -11.6 17))
; expect (10 2.5 11.6 17)
(map (lambda (x) (* x x))
(list 1 2 3 4))
; expect (1 4 9 16)
(define (scale-list items factor)
(map (lambda (x) (* x factor))
items))
(scale-list (list 1 2 3 4 5) 10)
; expect (10 20 30 40 50)
(define (count-leaves x)
(cond ((null? x) 0)
((not (pair? x)) 1)
(else (+ (count-leaves (car x))
(count-leaves (cdr x))))))
(define x (cons (list 1 2) (list 3 4)))
(count-leaves x)
; expect 4
(count-leaves (list x x))
; expect 8
;;; 2.2.3
(define (odd? x) (= 1 (remainder x 2)))
(define (filter predicate sequence)
(cond ((null? sequence) nil)
((predicate (car sequence))
(cons (car sequence)
(filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))
(filter odd? (list 1 2 3 4 5))
; expect (1 3 5)
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(accumulate + 0 (list 1 2 3 4 5))
; expect 15
(accumulate * 1 (list 1 2 3 4 5))
; expect 120
(accumulate cons nil (list 1 2 3 4 5))
; expect (1 2 3 4 5)
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(enumerate-interval 2 7)
; expect (2 3 4 5 6 7)
(define (enumerate-tree tree)
(cond ((null? tree) nil)
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))
(enumerate-tree (list 1 (list 2 (list 3 4)) 5))
; expect (1 2 3 4 5)
;;; 2.3.1
(define a 1)
(define b 2)
(list a b)
; expect (1 2)
(list 'a 'b)
; expect (a b)
(list 'a b)
; expect (a 2)
(car '(a b c))
; expect a
(cdr '(a b c))
; expect (b c)
(define (memq item x)
(cond ((null? x) #f)
((equal? item (car x)) x)
(else (memq item (cdr x)))))
(memq 'apple '(pear banana prune))
; expect #f
(memq 'apple '(x (apple sauce) y apple pear))
; expect (apple pear)
(define (my-equal? x y)
(cond ((pair? x) (and (pair? y)
(my-equal? (car x) (car y))
(my-equal? (cdr x) (cdr y))))
((null? x) (null? y))
(else (equal? x y))))
(my-equal? '(1 2 (three)) '(1 2 (three)))
; expect #t
(my-equal? '(1 2 (three)) '(1 2 three))
; expect #f
(my-equal? '(1 2 three) '(1 2 (three)))
; expect #f
;;; Peter Norvig tests (http://norvig.com/lispy2.html)
(define double (lambda (x) (* 2 x)))
(double 5)
; expect 10
(define compose (lambda (f g) (lambda (x) (f (g x)))))
((compose list double) 5)
; expect (10)
(define apply-twice (lambda (f) (compose f f)))
((apply-twice double) 5)
; expect 20
((apply-twice (apply-twice double)) 5)
; expect 80
(define fact (lambda (n) (if (<= n 1) 1 (* n (fact (- n 1))))))
(fact 3)
; expect 6
(fact 50)
; expect 30414093201713378043612608166064768844377641568960512000000000000
(define (combine f)
(lambda (x y)
(if (null? x) nil
(f (list (car x) (car y))
((combine f) (cdr x) (cdr y))))))
(define zip (combine cons))
(zip (list 1 2 3 4) (list 5 6 7 8))
; expect ((1 5) (2 6) (3 7) (4 8))
(define riff-shuffle (lambda (deck) (begin
(define take (lambda (n seq) (if (<= n 0) (quote ()) (cons (car seq) (take (- n 1) (cdr seq))))))
(define drop (lambda (n seq) (if (<= n 0) seq (drop (- n 1) (cdr seq)))))
(define mid (lambda (seq) (/ (length seq) 2)))
((combine append) (take (mid deck) deck) (drop (mid deck) deck)))))
(riff-shuffle (list 1 2 3 4 5 6 7 8))
; expect (1 5 2 6 3 7 4 8)
((apply-twice riff-shuffle) (list 1 2 3 4 5 6 7 8))
; expect (1 3 5 7 2 4 6 8)
(riff-shuffle (riff-shuffle (riff-shuffle (list 1 2 3 4 5 6 7 8))))
; expect (1 2 3 4 5 6 7 8)
;;; Additional tests
(apply square '(2))
; expect 4
(apply + '(1 2 3 4))
; expect 10
(apply (if #f + append) '((1 2) (3 4)))
; expect (1 2 3 4)
(if 0 1 2)
; expect 1
(if '() 1 2)
; expect 1
(or #f #t)
; expect #t
(or)
; expect #f
(and)
; expect #t
(or 1 2 3)
; expect 1
(and 1 2 3)
; expect 3
(and #f (/ 1 0))
; expect #f
(and #t (/ 1 0))
; expect Error
(or 3 (/ 1 0))
; expect 3
(or #f (/ 1 0))
; expect Error
(or (quote hello) (quote world))
; expect hello
(if nil 1 2)
; expect 1
(if 0 1 2)
; expect 1
(if (or #f #f #f) 1 2)
; expect 2
(define (loop) (loop))
(cond (#f (loop))
(12))
; expect 12
((lambda (x) (display x) (newline) x) 2)
; expect 2 ; 2
(let ((x 2)) ((begin (define x (+ x 1)) +) 3 (begin (define x (+ x 1)) x)))
; expect 7
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Scheme Implementations ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; len outputs the length of list s
(define (len s)
(if (eq? s '())
0
(+ 1 (len (cdr s)))))
(len '(1 2 3 4))
; expect 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Tests from Doctests ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(begin 1)
; expect 1
(begin 1 2)
; expect 2
(define x (begin (print 1) 2))
; expect 1 ; x
x
; expect 2
(define x 2)
; expect x
x
; expect 2
(define x (+ 2 8))
; expect x
x
; expect 10
(define (f x) (+ x 2))
; expect f
(f 3)
; expect 5
(quote (+ x 2))
; expect (+ x 2)
(lambda (x) (+ x 2))
; expect (lambda (x) (+ x 2))
(if #t (print 2) (print 3))
; expect 2
(if #f (print 2) (print 3))
; expect 3
(and #f (print 1))
; expect #f
(and (print 1) (print 2) (print 3) (print 4) 3 #f)
; expect 1; 2; 3; 4; #f
(or 10 (print 1))
; expect 10
(or #f 2 3 #t #f)
; expect 2
(cond (#f (print 2)) (#t (print 3)))
; expect 3
(let ((x 2) (y 3)) (+ x y))
; expect 5
;;;;;;;;;;;;;;;;;;;;
;;; Optional ;;;
;;;;;;;;;;;;;;;;;;;;
(exit)
; Tail call optimization tests
(define (sum n total)
(if (zero? n) total
(sum (- n 1) (+ n total))))
(sum 1001 0)
; expect 501501
(define (sum n total)
(cond ((zero? n) total)
(else (sum (- n 1) (+ n total)))))
(sum 1001 0)
; expect 501501
(define (sum n total)
(begin 2 3
(if (zero? n) total
(and 2 3
(or #f
(begin 2 3
(let ((m n))
(sum (- m 1) (+ m total)))))))))
(sum 1001 0)
; expect 501501
(exit)
; macro tests
(define (map f lst)
(if (null? lst)
nil
(cons
(f (car lst))
(map f (cdr lst)))))
(define-macro (for formal iterable body)
(list 'map (list 'lambda (list formal) body) iterable))
(for i '(1 2 3)
(if (= i 1)
0
i))
; expect (0 2 3)
(define (cadr s) (car (cdr s)))
(define (cars s) (map car s))
(define (cadrs s) (map cadr s))
(define-macro (leet bindings expr)
(cons
(list 'lambda (cars bindings) expr)
(cadrs bindings)))
(define (square x) (* x x))
(define (hyp a b)
(leet ((a2 (square a)) (b2 (square b))) (sqrt (+ a2 b2))))
(hyp 3 4)
; expect 5.000023178253949
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Optional Tests from Doctests ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-macro (f x) (car x))
; expect f
(f (1 2))
; expect 1