Quellcode durchsuchen

Complete next 8 lessons.

Lily Carpenter vor 9 Jahren
Ursprung
Commit
ccdc9c1e9e

+ 13 - 13
koans/condition-handlers.lsp

@@ -23,22 +23,22 @@ error handling code from normal operational code."
23 23
 
24 24
 (define-test test-conditions-derive-from-types
25 25
     "conditions inherit from base types"
26
-  (true-or-false? ___ (typep (make-condition 'minimal-error-cond)
26
+  (true-or-false? t (typep (make-condition 'minimal-error-cond)
27 27
                              'minimal-error-cond))
28 28
 
29
-  (true-or-false? ___ (typep (make-condition 'minimal-error-cond)
29
+  (true-or-false? t (typep (make-condition 'minimal-error-cond)
30 30
                              'error))
31 31
 
32
-  (true-or-false? ___ (typep (make-condition 'minimal-error-cond)
32
+  (true-or-false? nil (typep (make-condition 'minimal-error-cond)
33 33
                              'warning))
34 34
 
35
-  (true-or-false? ___ (typep (make-condition 'minimal-warning-cond)
35
+  (true-or-false? t (typep (make-condition 'minimal-warning-cond)
36 36
                              'minimal-warning-cond))
37 37
 
38
-  (true-or-false? ___ (typep (make-condition 'minimal-warning-cond)
38
+  (true-or-false? nil (typep (make-condition 'minimal-warning-cond)
39 39
                              'error))
40 40
 
41
-  (true-or-false? ___ (typep (make-condition 'minimal-warning-cond)
41
+  (true-or-false? t (typep (make-condition 'minimal-warning-cond)
42 42
                              'warning)))
43 43
 
44 44
 
@@ -60,21 +60,21 @@ error handling code from normal operational code."
60 60
     "assert-error checks that the right error is thrown"
61 61
   (assert-equal 3 (my-divide 6 2))
62 62
   (assert-error 'my-div-by-zero-error (my-divide 6 0))
63
-  (assert-error ____ (my-divide 6 "zero")))
63
+  (assert-error 'my-non-number-args-error (my-divide 6 "zero")))
64 64
 
65 65
 
66 66
 (define-test test-handle-errors
67 67
     "the handler case is like a case statement which can capture errors
68 68
      and warnings, and execute appropriate forms in those conditions."
69
-  (assert-equal ___
69
+  (assert-equal 3
70 70
                 (handler-case (my-divide 6 2)
71 71
                   (my-div-by-zero-error (condition) :zero-div-error)
72 72
                   (my-non-number-args-error (condition) :bad-args)))
73
-  (assert-equal ___
73
+  (assert-equal :zero-div-error
74 74
                 (handler-case (my-divide 6 0)
75 75
                   (my-div-by-zero-error (condition) :zero-div-error)
76 76
                   (my-non-number-args-error (condition) :bad-args)))
77
-  (assert-equal ___
77
+  (assert-equal :bad-args
78 78
                 (handler-case (my-divide 6 "woops")
79 79
                   (my-div-by-zero-error (condition) :zero-div-error)
80 80
                   (my-non-number-args-error (condition) :bad-args))))
@@ -112,13 +112,13 @@ http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node312.html"
112 112
 
113 113
 
114 114
 (define-test test-errors-have-slots
115
-    (assert-equal ____
115
+    (assert-equal :timestamp-logline-type
116 116
                   (handler-case (get-logline-type "TIMESTAMP y13m01d03")
117 117
                     (logline-parse-error (condition) (list (reason condition) (original-line condition)))))
118
-    (assert-equal ____
118
+    (assert-equal :http-logline-type
119 119
                   (handler-case (get-logline-type "HTTP access 128.0.0.100")
120 120
                     (logline-parse-error (condition) (list (reason condition) (original-line condition)))))
121
-    (assert-equal ____
121
+    (assert-equal
122 122
                   (handler-case (get-logline-type "bogus logline")
123 123
                     (logline-parse-error (condition) (list (reason condition) (original-line condition)))))
124 124
     (assert-equal ____

+ 9 - 9
koans/control-statements.lsp

@@ -17,11 +17,11 @@
17 17
       (if t
18 18
           (setf result "true value")
19 19
           (setf result "false value"))
20
-      (assert-equal result ____)
20
+      (assert-equal result "true value")
21 21
       (if nil
22 22
           (setf result "true value")
23 23
           (setf result "false value"))
24
-      (assert-equal result ____)))
24
+      (assert-equal result "false value")))
25 25
 
26 26
 
27 27
 (define-test test-when-and-unless
@@ -36,16 +36,16 @@
36 36
         (unless (> x 5)
37 37
           (setf result-2 x)
38 38
           (push x unless-nums)))
39
-      (assert-equal result-1 ___)
40
-      (assert-equal result-2 ___)
41
-      (assert-equal when-nums ___)
42
-      (assert-equal unless-nums ___)))
39
+      (assert-equal result-1 10)
40
+      (assert-equal result-2 5)
41
+      (assert-equal when-nums '(10 9 8 7 6))
42
+      (assert-equal unless-nums '(5 4 3 2 1))))
43 43
 
44 44
 
45 45
 (define-test test-and-short-circuits
46 46
     "and only evaluates forms until one evaluates to nil"
47 47
   (assert-equal
48
-   ____
48
+   2
49 49
    (let ((x 0))
50 50
      (and
51 51
       (setf x (+ 1 x))
@@ -58,11 +58,11 @@
58 58
 (define-test test-or-also-short-circuits
59 59
     "or only evaluates until one argument evaluates to non-nil"
60 60
   (assert-equal
61
-   ____
61
+   1
62 62
    (let ((x 0))
63 63
      (or
64 64
       (setf x (+ 1 x))
65 65
       (setf x (+ 1 x))
66 66
       nil
67 67
       (setf x (+ 1 x)))
68
-     x)))
68
+     x)))

+ 46 - 46
koans/functions.lsp

@@ -20,7 +20,7 @@
20 20
 
21 21
 (define-test test-call-a-function
22 22
     "DEFUN defines global functions"
23
-  (assert-equal ___ (some-named-function 7 11)))
23
+  (assert-equal 18 (some-named-function 7 11)))
24 24
 
25 25
 
26 26
 (define-test test-shadow-a-function
@@ -30,8 +30,8 @@
30 30
    (assert-eq 18 (some-named-function 7 11))
31 31
    "flet binds a function to a name within a lexical environment"
32 32
    (flet ((some-named-function (a b) (* a b)))
33
-     (assert-equal ___ (some-named-function 7 11)))
34
-   (assert-equal ___  (some-named-function 7 11)))
33
+     (assert-equal 77 (some-named-function 7 11)))
34
+   (assert-equal 18  (some-named-function 7 11)))
35 35
 
36 36
 
37 37
 ; borrowed from Common Lisp The Language chapter 5.2.2
@@ -41,9 +41,9 @@
41 41
 
42 42
 (define-test test-optional-parameters
43 43
     "Optional parameters are filled in with their default value."
44
-   (assert-equal (func-with-opt-params :test-1 :test-2) ___)
45
-   (assert-equal (func-with-opt-params :test-1) ___)
46
-   (assert-equal (func-with-opt-params) ___))
44
+   (assert-equal (func-with-opt-params :test-1 :test-2) '(:test-1 :test-2))
45
+   (assert-equal (func-with-opt-params :test-1) '(:test-1 3))
46
+   (assert-equal (func-with-opt-params) '(2 3)))
47 47
 
48 48
 
49 49
 ;; ----
@@ -56,9 +56,9 @@
56 56
    "Common Lisp optional params may bind a symbol which indicate whether the
57 57
     value was provided or defaulted.  Each optional parameter binding has the
58 58
     form (var default-form supplied-p)."
59
-   (assert-equal (func-with-opt-params-and-indication :test-1 :test-2) ___)
60
-   (assert-equal (func-with-opt-params-and-indication :test-1) ___)
61
-   (assert-equal (func-with-opt-params-and-indication) ___))
59
+   (assert-equal (func-with-opt-params-and-indication :test-1 :test-2) '(:test-1 t :test-2 t))
60
+   (assert-equal (func-with-opt-params-and-indication :test-1) '(:test-1 t 3 nil))
61
+   (assert-equal (func-with-opt-params-and-indication) '(2 nil 3 nil)))
62 62
 
63 63
 
64 64
 ;; ----
@@ -70,9 +70,9 @@
70 70
 (define-test test-func-with-rest-params
71 71
   "With &rest, the remaining params, are handed in as a list.  Remaining
72 72
    arguments (possibly none) are collected into a list."
73
-  (assert-equal (func-with-rest-params) ___)
74
-  (assert-equal (func-with-rest-params 1) ___)
75
-   (assert-equal (func-with-rest-params 1 :two 333) ___))
73
+  (assert-equal (func-with-rest-params) nil)
74
+  (assert-equal (func-with-rest-params 1) '(1))
75
+   (assert-equal (func-with-rest-params 1 :two 333) '(1 :two 333)))
76 76
 
77 77
 
78 78
 ;; ----
@@ -83,24 +83,24 @@
83 83
 
84 84
 (define-test test-key-params ()
85 85
   "Key params allow the user to specify params in any order"
86
-   (assert-equal (func-with-key-params) ___)
87
-   (assert-equal (func-with-key-params :a 11 :b 22) ___)
86
+   (assert-equal (func-with-key-params) '(nil nil))
87
+   (assert-equal (func-with-key-params :a 11 :b 22) '(11 22))
88 88
    ; it is not necessary to specify all key parameters
89
-   (assert-equal (func-with-key-params :b 22) ___)
89
+   (assert-equal (func-with-key-params :b 22) '(nil 22))
90 90
    ; order is not important
91
-   (assert-equal (func-with-key-params :b 22 :a 0) ___))
91
+   (assert-equal (func-with-key-params :b 22 :a 0) '(0 22)))
92 92
 
93 93
 (defun func-key-params-can-have-defaults (&key  (a 3 a?) (b 4 b?))
94 94
   (list a a? b b?))
95 95
 
96 96
 (define-test test-key-params-can-have-defaults
97 97
     "key parameters can have defaults also"
98
-   (assert-equal (func-key-params-can-have-defaults) ____)
99
-   (assert-equal (func-key-params-can-have-defaults :a 3 :b 4) ___)
100
-   (assert-equal (func-key-params-can-have-defaults :a 11 :b 22) ___)
101
-   (assert-equal (func-key-params-can-have-defaults :b 22) ___)
98
+   (assert-equal (func-key-params-can-have-defaults) '(3 nil 4 nil))
99
+   (assert-equal (func-key-params-can-have-defaults :a 3 :b 4) '(3 t 4 t))
100
+   (assert-equal (func-key-params-can-have-defaults :a 11 :b 22) '(11 t 22 t))
101
+   (assert-equal (func-key-params-can-have-defaults :b 22) '(3 nil 22 t))
102 102
    ; order is not important
103
-   (assert-equal (func-key-params-can-have-defaults :b 22 :a 0) ___))
103
+   (assert-equal (func-key-params-can-have-defaults :b 22 :a 0) '(0 t 22 t)))
104 104
 
105 105
 
106 106
 ;; ----
@@ -112,10 +112,10 @@
112 112
 
113 113
 (define-test test-many-kinds-params
114 114
     "CL provides the programmer with more than enough rope to hang himself."
115
-   (assert-equal (func-with-funky-parameters 1) ___)
116
-   (assert-equal (func-with-funky-parameters 1 :b 2) ___)
117
-   (assert-equal (func-with-funky-parameters 1 :b 2 :c 3) ___)
118
-   (assert-equal (func-with-funky-parameters 1 :c 3 :b 2) ___))
115
+  (assert-equal (func-with-funky-parameters 1) '(1 nil 1 nil))
116
+  (assert-equal (func-with-funky-parameters 1 :b 2) '(1 2 1 (:b 2)))
117
+  (assert-equal (func-with-funky-parameters 1 :b 2 :c 3) '(1 2 3 (:b 2 :c 3)))
118
+  (assert-equal (func-with-funky-parameters 1 :c 3 :b 2) '(1 2 3 (:c 3 :b 2))))
119 119
 
120 120
 
121 121
 ;; Note that &rest parameters have to come before &key parameters.
@@ -129,16 +129,16 @@
129 129
    (assert-equal 19 ((lambda (a b) (+ a b)) 10 9))
130 130
   (let ((my-function))
131 131
     (setf my-function (lambda (a b) (* a b)))
132
-    (assert-equal ___ (funcall my-function 11 9)))
132
+    (assert-equal 99 (funcall my-function 11 9)))
133 133
   (let ((list-of-functions nil))
134 134
     (push (lambda (a b) (+ a b)) list-of-functions)
135 135
     (push (lambda (a b) (* a b)) list-of-functions)
136 136
     (push (lambda (a b) (- a b)) list-of-functions)
137
-    (assert-equal ___ (funcall (second list-of-functions) 2 33))))
137
+    (assert-equal 66 (funcall (second list-of-functions) 2 33))))
138 138
 
139 139
 (define-test test-lambdas-can-have-optional-params
140
-   (assert-equal ___ ((lambda (a &optional (b 100)) (+ a b)) 10 9))
141
-   (assert-equal ___ ((lambda (a &optional (b 100)) (+ a b)) 10)))
140
+   (assert-equal 19 ((lambda (a &optional (b 100)) (+ a b)) 10 9))
141
+   (assert-equal 110 ((lambda (a &optional (b 100)) (+ a b)) 10)))
142 142
 
143 143
 
144 144
 ; returns sign x
@@ -148,9 +148,9 @@
148 148
   1)
149 149
 
150 150
 (define-test test-return-from-function-early
151
-   (assert-equal (sign-of -5.5) ___)
152
-   (assert-equal (sign-of 0) ___)
153
-   (assert-equal (sign-of ___) 1))
151
+   (assert-equal (sign-of -5.5) -1)
152
+   (assert-equal (sign-of 0) 0)
153
+   (assert-equal (sign-of 201) 1))
154 154
 
155 155
 
156 156
 ;; ----
@@ -170,8 +170,8 @@
170 170
   (let ((add-100 (adder 100))
171 171
         (add-500 (adder 500)))
172 172
   "add-100 and add-500 now refer to different bindings to x"
173
-   (assert-equal ___ (funcall add-100 3))
174
-   (assert-equal ___ (funcall add-500 3))))
173
+   (assert-equal 103 (funcall add-100 3))
174
+   (assert-equal 503 (funcall add-500 3))))
175 175
 
176 176
 
177 177
 ;; ----
@@ -192,13 +192,13 @@
192 192
     "An illustration of how lexical closures may interact."
193 193
   (let ((tangled-funs-1 (two-funs 1))
194 194
         (tangled-funs-2 (two-funs 2)))
195
-     (assert-equal (funcall (first tangled-funs-1)) ___)
195
+     (assert-equal (funcall (first tangled-funs-1)) 1)
196 196
      (funcall (second tangled-funs-1) 0)
197
-     (assert-equal (funcall (first tangled-funs-1)) ___)
197
+     (assert-equal (funcall (first tangled-funs-1)) 0)
198 198
 
199
-     (assert-equal (funcall (first tangled-funs-2)) ___)
199
+     (assert-equal (funcall (first tangled-funs-2)) 2)
200 200
      (funcall (second tangled-funs-2) 100)
201
-     (assert-equal (funcall (first tangled-funs-2)) ___)))
201
+     (assert-equal (funcall (first tangled-funs-2)) 100)))
202 202
 
203 203
 
204 204
 (define-test test-apply-function-with-apply
@@ -209,13 +209,13 @@
209 209
     (setq f2 '-)
210 210
     (setq f3 'max)
211 211
 
212
-    (assert-equal ___ (apply f1 '(1 2)))
213
-    (assert-equal ___ (apply f2 '(1 2)))
212
+    (assert-equal 3 (apply f1 '(1 2)))
213
+    (assert-equal -1 (apply f2 '(1 2)))
214 214
 
215 215
     ; after the function name, the parameters are consed onto the front
216 216
     ; of the very last parameter
217
-    (assert-equal ___ (apply f1 1 2 '(3)))
218
-    (assert-equal ___ (apply f3 1 2 3 4 '()))))
217
+    (assert-equal 6 (apply f1 1 2 '(3)))
218
+    (assert-equal 4 (apply f3 1 2 3 4 '()))))
219 219
 
220 220
 
221 221
 (define-test test-apply-function-with-funcall
@@ -225,7 +225,7 @@
225 225
     (setq f1 '+)
226 226
     (setq f2 '-)
227 227
     (setq f3 'max)
228
-    (assert-equal ___ (funcall f1 1 2))
229
-    (assert-equal ___ (funcall f2 1 2))
230
-    (assert-equal ___ (funcall f1 1 2 3))
231
-    (assert-equal ___ (funcall f3 1 2 3 4))))
228
+    (assert-equal 3 (funcall f1 1 2))
229
+    (assert-equal -1 (funcall f2 1 2))
230
+    (assert-equal 6 (funcall f1 1 2 3))
231
+    (assert-equal 4 (funcall f3 1 2 3 4))))

+ 22 - 18
koans/hash-tables.lsp

@@ -20,10 +20,10 @@
20 20
     "make hash table with make-hash-table"
21 21
   (let ((my-hash-table))
22 22
     (setf my-hash-table (make-hash-table))
23
-    (true-or-false? ___ (typep my-hash-table 'hash-table))
24
-    (true-or-false? ___  (hash-table-p my-hash-table))
25
-    (true-or-false? ___  (hash-table-p (make-array '(3 3 3))))
26
-    (assert-equal ___ (hash-table-count my-hash-table))))
23
+    (true-or-false? t (typep my-hash-table 'hash-table))
24
+    (true-or-false? t  (hash-table-p my-hash-table))
25
+    (true-or-false? nil  (hash-table-p (make-array '(3 3 3))))
26
+    (assert-equal 0 (hash-table-count my-hash-table))))
27 27
 
28 28
 
29 29
 (define-test test-hash-table-access
@@ -37,11 +37,11 @@
37 37
 
38 38
   (setf (gethash 8 table-of-cube-roots) 2)
39 39
   (setf (gethash -3 table-of-cube-roots) -27)
40
-  (assert-equal ___ (gethash -3 table-of-cube-roots))
41
-  (assert-equal ___ (hash-table-count table-of-cube-roots))
40
+  (assert-equal -27 (gethash -3 table-of-cube-roots))
41
+  (assert-equal 3 (hash-table-count table-of-cube-roots))
42 42
 
43 43
   "accessing unset keys returns nil"
44
-  (assert-equal ___ (gethash 125 table-of-cube-roots))))
44
+  (assert-equal nil (gethash 125 table-of-cube-roots))))
45 45
 
46 46
 
47 47
 (define-test test-hash-key-equality
@@ -67,9 +67,9 @@
67 67
     (setf (gethash "one" hash-table-default) "uno")
68 68
 
69 69
     "count how many unique key-value pairs in each"
70
-    (assert-equal ___ (hash-table-count hash-table-eq))
71
-    (assert-equal ___ (hash-table-count hash-table-equal))
72
-    (assert-equal ___ (hash-table-count hash-table-default))))
70
+    (assert-equal 2 (hash-table-count hash-table-eq))
71
+    (assert-equal 1 (hash-table-count hash-table-equal))
72
+    (assert-equal 2 (hash-table-count hash-table-default))))
73 73
 
74 74
 
75 75
 (define-test test-hash-table-equality
@@ -79,9 +79,9 @@
79 79
       (setf (gethash "one" h2) "yat")
80 80
       (setf (gethash "two" h1) "yi")
81 81
       (setf (gethash "two" h2) "yi")
82
-      (true-or-false? ___ (eq h1 h2))
83
-      (true-or-false? ___ (equal h1 h2))
84
-      (true-or-false? ___ (equalp h1 h2))))
82
+      (true-or-false? nil (eq h1 h2))
83
+      (true-or-false? nil (equal h1 h2))
84
+      (true-or-false? t (equalp h1 h2))))
85 85
 
86 86
 
87 87
 (define-test test-changing-hash-tables
@@ -93,7 +93,7 @@
93 93
       (setf (gethash "two" expected) "zwei")
94 94
 
95 95
       (setf (gethash "one" babel-fish) "eins")
96
-      (setf (gethash "two" babel-fish) ____)
96
+      (setf (gethash "two" babel-fish) "zwei")
97 97
 
98 98
       (assert-true (equalp babel-fish expected))))
99 99
 
@@ -109,18 +109,22 @@
109 109
       (setf value-and-exists (multiple-value-list (gethash "Obama" prev-pres)))
110 110
       (assert-equal value-and-exists '("Bush" t))
111 111
       (setf value-and-exists (multiple-value-list (gethash "Lincoln" prev-pres)))
112
-      (assert-equal value-and-exists ____)
112
+      (assert-equal value-and-exists '("Buchanan" t))
113 113
       (setf value-and-exists (multiple-value-list (gethash "Washington" prev-pres)))
114
-      (assert-equal value-and-exists ____)
114
+      (assert-equal value-and-exists '(nil t))
115 115
       (setf value-and-exists (multiple-value-list (gethash "Franklin" prev-pres)))
116
-      (assert-equal value-and-exists ____)))
116
+      (assert-equal value-and-exists '(nil nil))))
117 117
 
118 118
 
119 119
 (define-test test-make-your-own-hash-table
120 120
     "make a hash table that meets the following conditions"
121
-  (let ((colors (make-hash-table))
121
+  (let ((colors (make-hash-table :test #'equal))
122 122
         (values (make-hash-table)))
123 123
 
124
+    (setf (gethash "blue" colors) '(0 0 1))
125
+    (setf (gethash "green" colors) '(0 1 0))
126
+    (setf (gethash "red" colors) '(1 0 0))
127
+    (setf (gethash "purple" colors) '(1 0 1))
124 128
     (assert-equal (hash-table-count colors) 4)
125 129
     (setf values (list (gethash "blue" colors)
126 130
                        (gethash "green" colors)

+ 12 - 11
koans/iteration.lsp

@@ -31,12 +31,13 @@
31 31
       (if (> one-prime biggest-in-list)
32 32
           (setf biggest-in-list one-prime))
33 33
       (incf how-many-in-list))
34
-    (assert-equal ___ how-many-in-list)
35
-    (assert-equal ___ biggest-in-list))
34
+    (assert-equal 4 how-many-in-list)
35
+    (assert-equal 999565999 biggest-in-list))
36 36
   (let ((sum 0))
37 37
     "write your own do-list here to calculate the sum of some-primes"
38 38
     "you may be interested in investigating the 'incf' function"
39
-    ;(dolist ... )
39
+    (dolist (x some-primes)
40
+      (incf sum x))
40 41
     (assert-equal 999607602 sum)))
41 42
 
42 43
 
@@ -47,7 +48,7 @@
47 48
           (my-return))
48 49
       (dolist (x my-list my-return)
49 50
         (push (* x x) my-return))
50
-      (assert-equal ____ my-return)))
51
+      (assert-equal '(16 9 4 1) my-return)))
51 52
 
52 53
 
53 54
 (define-test test-dotimes
@@ -55,7 +56,7 @@
55 56
       binding them in order to your selected symbol."
56 57
     (let ((out-list nil))
57 58
       (dotimes (y 3) (push y out-list))
58
-      (assert-equal out-list ___)))
59
+      (assert-equal out-list '(2 1 0))))
59 60
 
60 61
 
61 62
 (defvar *x* "global")
@@ -63,8 +64,8 @@
63 64
     "dotimes establishes a local lexical binding which may shadow
64 65
      a global value."
65 66
   (dotimes (*x* 4)
66
-    (true-or-false? ___ (equal "global" *x*)))
67
-  (true-or-false? ___ (equal "global" *x*)))
67
+    (true-or-false? nil (equal "global" *x*)))
68
+  (true-or-false? t (equal "global" *x*)))
68 69
 
69 70
 
70 71
 (define-test test-loop-until-return
@@ -75,14 +76,14 @@
75 76
       (loop
76 77
         (incf loop-counter)
77 78
         (if (>= loop-counter 100) (return loop-counter)))
78
-      (assert-equal ___ loop-counter)))
79
+      (assert-equal 100 loop-counter)))
79 80
 
80 81
 
81 82
 (define-test test-mapcar
82 83
     "mapcar takes a list an a function.  It returns a new list
83 84
      with the function applied to each element of the input"
84 85
   (let ((mc-result (mapcar #'evenp '(1 2 3 4 5))))
85
-    (assert-equal mc-result ____)))
86
+    (assert-equal mc-result '(nil t nil t nil))))
86 87
 
87 88
 
88 89
 ;; ----
@@ -106,11 +107,11 @@
106 107
   (assert-equal (vowels-to-xs "Astronomy") "xstrxnxmy")
107 108
   (let* ((subjects '("Astronomy" "Biology" "Chemistry" "Linguistics"))
108 109
          (mc-result (mapcar #'vowels-to-xs subjects)))
109
-    (assert-equal mc-result ____)))
110
+    (assert-equal mc-result '("xstrxnxmy" "Bxxlxgy" "Chxmxstry" "Lxngxxstxcs"))))
110 111
 
111 112
 
112 113
 ;; ----
113 114
 
114 115
 (define-test test-mapcar-with-lambda
115 116
     (let ((mc-result (mapcar (lambda (x) (mod x 10)) '(21 152 403 14))))
116
-      (assert-equal mc-result ____)))
117
+      (assert-equal mc-result '(1 2 3 4))))

+ 18 - 18
koans/mapcar-and-reduce.lsp

@@ -16,9 +16,9 @@
16 16
     "We can apply a function to each member
17 17
      of a list using mapcar."
18 18
   (defun times-two (x) (* x 2))
19
-  (assert-equal ____ (mapcar #'times-two '(1 2 3)))
20
-  (assert-equal ____ (mapcar #'first '((3 2 1) 
21
-                                      ("little" "small" "tiny") 
19
+  (assert-equal '(2 4 6) (mapcar #'times-two '(1 2 3)))
20
+  (assert-equal '(3 "little" "pigs") (mapcar #'first '((3 2 1)
21
+                                      ("little" "small" "tiny")
22 22
                                       ("pigs" "hogs" "swine")))))
23 23
 
24 24
 
@@ -26,20 +26,20 @@
26 26
     "The mapcar function can be applied to
27 27
      more than one list. It applies a function
28 28
      to successive elements of the lists."
29
-  (assert-equal ____ (mapcar #'* '(1 2 3) '(4 5 6)))
30
-  (assert-equal ____ (mapcar #'list '("lisp" "are") '("koans" "fun"))))
29
+  (assert-equal '(4 10 18) (mapcar #'* '(1 2 3) '(4 5 6)))
30
+  (assert-equal '(("lisp" "koans") ("are" "fun")) (mapcar #'list '("lisp" "are") '("koans" "fun"))))
31 31
 
32 32
 
33 33
 (define-test test-transpose-using-mapcar
34 34
     "Replace WRONG-FUNCTION with the correct function (don't forget
35 35
      the #') to take the 'transpose'."
36
-  (defun WRONG-FUNCTION-1 (&rest rest) '())
36
+  (defun WRONG-FUNCTION-1 (&rest rest) rest)
37 37
   (defun transpose (L) (apply #'mapcar (cons #'WRONG-FUNCTION-1 L)))
38 38
     (assert-equal '((1 4 7)
39
-                  (2 5 8) 
40
-                  (3 6 9)) 
41
-                (transpose '((1 2 3) 
42
-                             (4 5 6) 
39
+                  (2 5 8)
40
+                  (3 6 9))
41
+                (transpose '((1 2 3)
42
+                             (4 5 6)
43 43
                              (7 8 9))))
44 44
   (assert-equal '(("these" "pretzels" "are")
45 45
                   ("making" "me" "thirsty"))
@@ -52,21 +52,21 @@
52 52
     "The reduce function applies uses a supplied
53 53
      binary function to combine the elements of a
54 54
      list from left to right."
55
-  (assert-equal ___  (reduce #'+ '(1 2 3 4)))
56
-  (assert-equal ___ (reduce #'expt '(2 3 2))))
55
+  (assert-equal 10  (reduce #'+ '(1 2 3 4)))
56
+  (assert-equal 64 (reduce #'expt '(2 3 2))))
57 57
 
58 58
 
59 59
 (define-test test-reduce-right-to-left
60 60
     "The keyword :from-end allows us to apply
61 61
      reduce from right to left."
62
-  (assert-equal ___ (reduce #'+ '(1 2 3 4) :from-end t))
63
-  (assert-equal ___ (reduce #'expt '(2 3 2) :from-end t)))
62
+  (assert-equal 10 (reduce #'+ '(1 2 3 4) :from-end t))
63
+  (assert-equal 512 (reduce #'expt '(2 3 2) :from-end t)))
64 64
 
65 65
 
66 66
 (define-test test-reduce-with-initial-value
67 67
     "We can supply an initial value to reduce."
68
-  (assert-equal ___ (reduce #'expt '(10 21 34 43) :initial-value 1))
69
-  (assert-equal ___ (reduce #'expt '(10 21 34 43) :initial-value 0)))
68
+  (assert-equal 1 (reduce #'expt '(10 21 34 43) :initial-value 1))
69
+  (assert-equal 0 (reduce #'expt '(10 21 34 43) :initial-value 0)))
70 70
 
71 71
 
72 72
 (defun WRONG-FUNCTION-2 (a b) (a))
@@ -76,7 +76,7 @@
76 76
     "mapcar and reduce are a powerful combination.
77 77
      insert the correct function names, instead of WRONG-FUNCTION-X
78 78
      to define an inner product."
79
-  (defun inner (x y) 
80
-    (reduce #'WRONG-FUNCTION-2 (mapcar #'WRONG-FUNCTION-3 x y)))
79
+  (defun inner (x y)
80
+    (reduce #'+ (mapcar #'* x y)))
81 81
   (assert-equal 32 (inner '(1 2 3) '(4 5 6)))
82 82
   (assert-equal 310 (inner '(10 20 30) '(4 3 7))))

+ 16 - 17
koans/strings.lsp

@@ -14,24 +14,24 @@
14 14
 
15 15
 (define-test test-double-quoted-strings-are-strings
16 16
     (let ((my-string "do or do not"))
17
-      (true-or-false? ___ (typep my-string 'string))
17
+      (true-or-false? t (typep my-string 'string))
18 18
       "strings are the same thing as vectors of characters"
19
-      (true-or-false? ___ (typep my-string 'array))
19
+      (true-or-false? t (typep my-string 'array))
20 20
       (assert-equal (aref "meat" 2) (aref "fiesta" 5))
21 21
       "strings are not integers :p"
22
-      (true-or-false? ___ (typep my-string 'integer))))
22
+      (true-or-false? nil (typep my-string 'integer))))
23 23
 
24 24
 
25 25
 (define-test test-multi-line-strings-are-strings
26 26
     (let ((my-string "this is
27 27
                       a multi
28 28
                       line string"))
29
-      (true-or-false? ___ (typep my-string 'string))))
29
+      (true-or-false? t (typep my-string 'string))))
30 30
 
31 31
 
32 32
 (define-test test-escape-quotes
33 33
     (let ((my-string "this string has one of these \" in it"))
34
-      (true-or-false? ___ (typep my-string 'string))))
34
+      (true-or-false? t (typep my-string 'string))))
35 35
 
36 36
 
37 37
 ; This test from common lisp cookbook
@@ -39,19 +39,19 @@
39 39
     "since strings are sequences, you may use subseq"
40 40
   (let ((my-string "Groucho Marx"))
41 41
     (assert-equal "Marx" (subseq my-string 8))
42
-    (assert-equal (subseq my-string 0 7) ____)
43
-    (assert-equal (subseq my-string 1 5) ____)))
42
+    (assert-equal (subseq my-string 0 7) "Groucho")
43
+    (assert-equal (subseq my-string 1 5) "rouc")))
44 44
 
45 45
 (define-test test-accessing-individual-characters
46 46
   "char literals look like this"
47
-  (true-or-false? ___ (typep #\a 'character))
48
-  (true-or-false? ___ (typep "A" 'character))
49
-  (true-or-false? ___ (typep #\a 'string))
47
+  (true-or-false? t (typep #\a 'character))
48
+  (true-or-false? nil (typep "A" 'character))
49
+  (true-or-false? nil (typep #\a 'string))
50 50
   "char is used to access individual characters"
51 51
   (let ((my-string "Cookie Monster"))
52 52
     (assert-equal (char my-string 0) #\C)
53 53
     (assert-equal (char my-string 3) #\k)
54
-    (assert-equal (char my-string 7) ___)))
54
+    (assert-equal (char my-string 7) #\M)))
55 55
 
56 56
 
57 57
 (define-test test-concatenating-strings
@@ -59,20 +59,19 @@
59 59
   (let ((a "this")
60 60
         (b "is")
61 61
         (c "unwieldy"))
62
-    (assert-equal ___ (concatenate 'string a " " b " " c))))
62
+    (assert-equal "this is unwieldy" (concatenate 'string a " " b " " c))))
63 63
 
64 64
 
65 65
 (define-test test-searching-for-characters
66 66
     "you can use position to detect characters in strings
67 67
      (or elements of sequences)"
68
-  (assert-equal ___ (position #\b "abc"))
69
-  (assert-equal ___ (position #\c "abc"))
70
-  (assert-equal ___ (find #\d "abc")))
68
+  (assert-equal 1 (position #\b "abc"))
69
+  (assert-equal 2 (position #\c "abc"))
70
+  (assert-equal nil (find #\d "abc")))
71 71
 
72 72
 
73 73
 (define-test test-finding-substrings
74 74
     "search finds subsequences"
75 75
   (let ((title "A supposedly fun thing I'll never do again"))
76 76
     (assert-equal 2 (search "supposedly" title))
77
-    (assert-equal 12 (search "CHANGETHISWORD" title))))
78
-
77
+    (assert-equal 12 (search " fun" title))))

+ 18 - 19
koans/structures.lsp

@@ -31,11 +31,11 @@
31 31
   (let ((player-1 (make-basketball-player
32 32
                    :name "larry" :team :celtics :number 33)))
33 33
     (assert-equal "larry" (basketball-player-name player-1))
34
-    (assert-equal ___ (basketball-player-team player-1))
35
-    (assert-equal ___ (basketball-player-number player-1))
34
+    (assert-equal :celtics (basketball-player-team player-1))
35
+    (assert-equal 33 (basketball-player-number player-1))
36 36
     (assert-equal 'basketball-player (type-of player-1))
37 37
     (setf (basketball-player-team player-1) :RETIRED)
38
-    (assert-equal ___ (basketball-player-team player-1))))
38
+    (assert-equal :RETIRED (basketball-player-team player-1))))
39 39
 
40 40
 
41 41
 ;; Struct fields can have default values
@@ -45,9 +45,9 @@
45 45
 
46 46
 (define-test test-struct-defaults
47 47
     (let ((player-2 (make-baseball-player)))
48
-      (assert-equal ___ (baseball-player-position player-2))
49
-      (assert-equal ___ (baseball-player-team player-2))
50
-      (assert-equal ___ (baseball-player-name player-2))))
48
+      (assert-equal :outfield (baseball-player-position player-2))
49
+      (assert-equal :red-sox (baseball-player-team player-2))
50
+      (assert-equal nil (baseball-player-name player-2))))
51 51
 
52 52
 
53 53
 ;; The accessor names can get pretty long.  It's possible to specify
@@ -58,8 +58,7 @@
58 58
 (define-test test-abbreviated-struct-access
59 59
     (let ((player-3 (make-american-football-player
60 60
                      :name "Drew Brees" :position :QB :team "Saints")))
61
-      (assert-equal ___ (nfl-guy-position player-3))))
62
-
61
+      (assert-equal :QB (nfl-guy-position player-3))))
63 62
 
64 63
 ;; Structs can be defined as EXTENSIONS to previous structures.
65 64
 ;; This form of inheritance allows composition of objects.
@@ -74,13 +73,13 @@
74 73
                        :name "Kobe Bryant"
75 74
                        :team :LAKERS
76 75
                        :number 24)))
77
-      (assert-equal ___ (nba-contract-start-year contract-1))
78
-      (assert-equal ___ (type-of contract-1))
76
+      (assert-equal 2004 (nba-contract-start-year contract-1))
77
+      (assert-equal 'NBA-CONTRACT (type-of contract-1))
79 78
       ;; do inherited structures follow the rules of type hierarchy?
80
-      (true-or-false? ___ (typep contract-1 'BASKETBALL-PLAYER))
79
+      (true-or-false? t (typep contract-1 'BASKETBALL-PLAYER))
81 80
       ;; can you access structure fields with the inherited accessors?
82
-      (assert-equal ___ (nba-contract-team contract-1))
83
-      (assert-equal ___ (basketball-player-team contract-1))))
81
+      (assert-equal :LAKERS (nba-contract-team contract-1))
82
+      (assert-equal :LAKERS (basketball-player-team contract-1))))
84 83
 
85 84
 
86 85
 ;; Copying of structs is handled with the copy-{name} form.  Note that
@@ -90,15 +89,15 @@
90 89
     (let ((manning-1 (make-american-football-player :name "Manning" :team '("Colts" "Broncos")))
91 90
           (manning-2 (make-american-football-player :name "Manning" :team '("Colts" "Broncos"))))
92 91
       ;; manning-1 and manning-2 are different objects
93
-      (true-or-false? ___ (eq manning-1 manning-2))
92
+      (true-or-false? nil (eq manning-1 manning-2))
94 93
       ;; but manning-1 and manning-2 contain the same information
95 94
       ;; (note the equalp instead of eq
96
-      (true-or-false? ___ (equalp manning-1 manning-2))
95
+      (true-or-false? t (equalp manning-1 manning-2))
97 96
       ;; copied structs are much the same.
98
-      (true-or-false? ___ (equalp manning-1 (copy-american-football-player manning-1)))
99
-      (true-or-false? ___ (eq     manning-1 (copy-american-football-player manning-1)))
97
+      (true-or-false? t (equalp manning-1 (copy-american-football-player manning-1)))
98
+      (true-or-false? nil (eq     manning-1 (copy-american-football-player manning-1)))
100 99
       ;; note that the copying is shallow
101 100
       (let ((shallow-copy (copy-american-football-player manning-1)))
102 101
         (setf (car (nfl-guy-team manning-1)) "Giants")
103
-        (assert-equal ___ (car (nfl-guy-team manning-1)))
104
-        (assert-equal ___ (car (nfl-guy-team shallow-copy))))))
102
+        (assert-equal "Giants" (car (nfl-guy-team manning-1)))
103
+        (assert-equal "Giants" (car (nfl-guy-team shallow-copy))))))