| 1 |
|
| 2 |
|
| 3 |
(add-tests-with-string-output "letrec" |
| 4 |
[(letrec () 12) => "12\n"] |
| 5 |
[(letrec ([f 12]) f) => "12\n"] |
| 6 |
[(letrec ([f 12] [g 13]) (fx+ f g)) => "25\n"] |
| 7 |
[(letrec ([fact |
| 8 |
(lambda (n) |
| 9 |
(if (fxzero? n) |
| 10 |
1 |
| 11 |
(fx* n (fact (fxsub1 n)))))]) |
| 12 |
(fact 5)) => "120\n"] |
| 13 |
[(letrec ([f 12] [g (lambda () f)]) |
| 14 |
(g)) => "12\n"] |
| 15 |
[(letrec ([f 12] [g (lambda (n) (set! f n))]) |
| 16 |
(g 130) |
| 17 |
f) => "130\n"] |
| 18 |
[(letrec ([f (lambda (g) (set! f g) (f))]) |
| 19 |
(f (lambda () 12))) => "12\n"] |
| 20 |
[(letrec ([f (cons (lambda () f) |
| 21 |
(lambda (x) (set! f x)))]) |
| 22 |
(let ([g (car f)]) |
| 23 |
((cdr f) 100) |
| 24 |
(g))) => "100\n"] |
| 25 |
[(letrec ([f (letrec ([g (lambda (x) (fx* x 2))]) |
| 26 |
(lambda (n) (g (fx* n 2))))]) |
| 27 |
(f 12)) => "48\n"] |
| 28 |
[(letrec ([f (lambda (f n) |
| 29 |
(if (fxzero? n) |
| 30 |
1 |
| 31 |
(fx* n (f f (fxsub1 n)))))]) |
| 32 |
(f f 5)) => "120\n"] |
| 33 |
[(let ([f (lambda (f) |
| 34 |
(lambda (n) |
| 35 |
(if (fxzero? n) |
| 36 |
1 |
| 37 |
(fx* n (f (fxsub1 n))))))]) |
| 38 |
(letrec ([fix |
| 39 |
(lambda (f) |
| 40 |
(f (lambda (n) ((fix f) n))))]) |
| 41 |
((fix f) 5))) => "120\n"] |
| 42 |
) |
| 43 |
|
| 44 |
#;(add-tests-with-string-output "letrec*" |
| 45 |
[(letrec* () 12) => "12\n"] |
| 46 |
[(letrec* ([f 12]) f) => "12\n"] |
| 47 |
[(letrec* ([f 12] [g 13]) (fx+ f g)) => "25\n"] |
| 48 |
[(letrec* ([fact |
| 49 |
(lambda (n) |
| 50 |
(if (fxzero? n) |
| 51 |
1 |
| 52 |
(fx* n (fact (fxsub1 n)))))]) |
| 53 |
(fact 5)) => "120\n"] |
| 54 |
[(letrec* ([f 12] [g (lambda () f)]) |
| 55 |
(g)) => "12\n"] |
| 56 |
[(letrec* ([f 12] [g (lambda (n) (set! f n))]) |
| 57 |
(g 130) |
| 58 |
f) => "130\n"] |
| 59 |
[(letrec* ([f (lambda (g) (set! f g) (f))]) |
| 60 |
(f (lambda () 12))) => "12\n"] |
| 61 |
[(letrec* ([f (cons (lambda () f) |
| 62 |
(lambda (x) (set! f x)))]) |
| 63 |
(let ([g (car f)]) |
| 64 |
((cdr f) 100) |
| 65 |
(g))) => "100\n"] |
| 66 |
[(letrec* ([f (letrec* ([g (lambda (x) (fx* x 2))]) |
| 67 |
(lambda (n) (g (fx* n 2))))]) |
| 68 |
(f 12)) => "48\n"] |
| 69 |
[(letrec* ([f (lambda (f n) |
| 70 |
(if (fxzero? n) |
| 71 |
1 |
| 72 |
(fx* n (f f (fxsub1 n)))))]) |
| 73 |
(f f 5)) => "120\n"] |
| 74 |
[(let ([f (lambda (f) |
| 75 |
(lambda (n) |
| 76 |
(if (fxzero? n) |
| 77 |
1 |
| 78 |
(fx* n (f (fxsub1 n))))))]) |
| 79 |
(letrec* ([fix |
| 80 |
(lambda (f) |
| 81 |
(f (lambda (n) ((fix f) n))))]) |
| 82 |
((fix f) 5))) => "120\n"] |
| 83 |
[(letrec* ([a 12] [b (fx+ a 5)] [c (fx+ b a)]) |
| 84 |
c) => "29\n"] |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
(add-tests-with-string-output "and/or" |
| 89 |
[(and) => "#t\n"] |
| 90 |
[(and 5) => "5\n"] |
| 91 |
[(and #f) => "#f\n"] |
| 92 |
[(and 5 6) => "6\n"] |
| 93 |
[(and #f ((lambda (x) (x x)) (lambda (x) (x x)))) => "#f\n"] |
| 94 |
[(or) => "#f\n"] |
| 95 |
[(or #t) => "#t\n"] |
| 96 |
[(or 5) => "5\n"] |
| 97 |
[(or 1 2 3) => "1\n"] |
| 98 |
[(or (cons 1 2) ((lambda (x) (x x)) (lambda (x) (x x)))) => "(1 . 2)\n"] |
| 99 |
[(let ([if 12]) (or if 17)) => "12\n"] |
| 100 |
[(let ([if 12]) (and if 17)) => "17\n"] |
| 101 |
[(let ([let 8]) (or let 18)) => "8\n"] |
| 102 |
[(let ([let 8]) (and let 18)) => "18\n"] |
| 103 |
[(let ([t 1]) |
| 104 |
(and (begin (set! t (fxadd1 t)) t) t)) => "2\n"] |
| 105 |
[(let ([t 1]) |
| 106 |
(or (begin (set! t (fxadd1 t)) t) t)) => "2\n"] |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
(add-tests-with-string-output "when/unless" |
| 111 |
[(let ([x (cons 1 2)]) |
| 112 |
(when (pair? x) |
| 113 |
(set-car! x (fx+ (car x) (cdr x)))) |
| 114 |
x) => "(3 . 2)\n"] |
| 115 |
[(let ([x (cons 1 2)]) |
| 116 |
(when (pair? x) |
| 117 |
(set-car! x (fx+ (car x) (cdr x))) |
| 118 |
(set-car! x (fx+ (car x) (cdr x)))) |
| 119 |
x) => "(5 . 2)\n"] |
| 120 |
[(let ([x (cons 1 2)]) |
| 121 |
(unless (fixnum? x) |
| 122 |
(set-car! x (fx+ (car x) (cdr x)))) |
| 123 |
x) => "(3 . 2)\n"] |
| 124 |
[(let ([x (cons 1 2)]) |
| 125 |
(unless (fixnum? x) |
| 126 |
(set-car! x (fx+ (car x) (cdr x))) |
| 127 |
(set-car! x (fx+ (car x) (cdr x)))) |
| 128 |
x) => "(5 . 2)\n"] |
| 129 |
[(let ([let 12]) |
| 130 |
(when let let let let let)) => "12\n"] |
| 131 |
[(let ([let #f]) |
| 132 |
(unless let let let let let)) => "#f\n"] |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
(add-tests-with-string-output "cond" |
| 137 |
[(cond [1 2] [else 3]) => "2\n"] |
| 138 |
[(cond [1] [else 13]) => "1\n"] |
| 139 |
[(cond [#f #t] [#t #f]) => "#f\n"] |
| 140 |
[(cond [else 17]) => "17\n"] |
| 141 |
[(cond [#f] [#f 12] [12 13]) => "13\n"] |
| 142 |
[(cond [(cons 1 2) => (lambda (x) (cdr x))]) => "2\n"] |
| 143 |
[(let ([else #t]) |
| 144 |
(cond |
| 145 |
[else 1287])) => "1287\n"] |
| 146 |
[(let ([else 17]) |
| 147 |
(cond |
| 148 |
[else])) => "17\n"] |
| 149 |
[(let ([else 17]) |
| 150 |
(cond |
| 151 |
[else => (lambda (x) x)])) => "17\n"] |
| 152 |
[(let ([else #f]) |
| 153 |
(cond |
| 154 |
[else ((lambda (x) (x x)) (lambda (x) (x x)))]) |
| 155 |
else) => "#f\n"] |
| 156 |
[(let ([=> 12]) |
| 157 |
(cond |
| 158 |
[12 => 14] |
| 159 |
[else 17])) => "14\n"] |
| 160 |
[(let ([=> 12]) |
| 161 |
(cond |
| 162 |
[=>])) => "12\n"] |
| 163 |
[(let ([=> 12]) |
| 164 |
(cond |
| 165 |
[=> =>])) => "12\n"] |
| 166 |
[(let ([=> 12]) |
| 167 |
(cond |
| 168 |
[=> => =>])) => "12\n"] |
| 169 |
[(let ([let 12]) |
| 170 |
(cond |
| 171 |
[let => (lambda (x) (fx+ let x))] |
| 172 |
[else 14])) => "24\n"] |
| 173 |
) |
| 174 |
|