Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table

This commit is contained in:
venomade committed 2026-11-22 11:15:35 +00:00
1 parent 77843402fa
commit 4675bd1106
76 files changed
+2505 -14

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
;;; S-EXPRESSIONS 4: READER SHORTHAND (solution)
(defstruct point x y)
(defun the-adder ()
#'+)
(defun counting ()
#(1 2 3))
(defun origin ()
#s(point :x 1 :y 2))
(defun magic-web-number ()
#xdeadbeef)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (functionp (the-adder)))
(check (vectorp (counting)))
(check (point-p (origin)))
(check-equal 3735928559 (magic-web-number))
+17
View File
@@ -0,0 +1,17 @@
;;; FUNCTIONS 5: PRODUCING VALUES (solution)
(defun split-money (cents)
(values (floor cents 100) (mod cents 100)))
(defun half-and-double (n)
(values (/ n 2) (* n 2)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(2 65) (multiple-value-list (split-money 265)))
(check-equal '(0 5) (multiple-value-list (split-money 5)))
(check-equal '(3 12) (multiple-value-list (half-and-double 6)))
(check-equal '(7/2 14) (multiple-value-list (half-and-double 7)))
+26
View File
@@ -0,0 +1,26 @@
;;; CONDITIONALS 5: CASE AND ECASE (solution)
(defun rate-fruit (fruit)
(case fruit
((apple banana) "cheap")
((mango durian) "pricey")
(t "unknown")))
(defun door-state (state)
(ecase state
(:open "come in")
(:closed "knock first")))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "cheap" (rate-fruit 'apple))
(check-equal "cheap" (rate-fruit 'banana))
(check-equal "pricey" (rate-fruit 'mango))
(check-equal "pricey" (rate-fruit 'durian))
(check-equal "unknown" (rate-fruit 'kiwi))
(check-equal "come in" (door-state :open))
(check-equal "knock first" (door-state :closed))
(check-error (door-state :ajar) 'type-error)
+23
View File
@@ -0,0 +1,23 @@
;;; PREDICATES 4: EQL AND EQUALP (solution)
(defun same-exact-p (a b)
(eql a b))
(defun same-amount-p (a b)
(equalp a b))
(defun same-twin-p (a b)
(equalp a b))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (same-exact-p 7 7))
(check (not (same-exact-p 1 1.0)))
(check (not (same-exact-p 1 2)))
(check (same-amount-p 1 1.0))
(check (not (same-amount-p 1 2)))
(check (same-twin-p #\a #\A))
(check (not (same-twin-p #\a #\b)))
+19
View File
@@ -0,0 +1,19 @@
;;; LISTS 6: DESTRUCTURING-BIND (solution)
(defun dimensions (dims)
(destructuring-bind (w h d) dims
(* w h d)))
(defun first-and-rest (xs)
(destructuring-bind (x . rest) xs
(values x rest)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 60 (dimensions '(3 4 5)))
(check-equal 0 (dimensions '(0 10 2)))
(check-equal '(1 (2 3)) (multiple-value-list (first-and-rest '(1 2 3))))
(check-equal '(a ()) (multiple-value-list (first-and-rest '(a))))
+22
View File
@@ -0,0 +1,22 @@
;;; RECURSION 5: MUTUAL RECURSION (solution)
(defun my-evenp (n)
(labels ((even-step (k)
(if (zerop k)
t
(odd-step (1- k))))
(odd-step (k)
(if (zerop k)
nil
(even-step (1- k)))))
(even-step n)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (my-evenp 0))
(check (my-evenp 4))
(check (not (my-evenp 7)))
(check (not (my-evenp 1)))
+17
View File
@@ -0,0 +1,17 @@
;;; STRINGS 5: NUMBERS IN FORMAT (solution)
(defun price-tag (n)
(format nil "~:d widgets" n))
(defun fixed (label n)
(format nil "~a is about ~,2f" label n))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "1,234,567 widgets" (price-tag 1234567))
(check-equal "7 widgets" (price-tag 7))
(check-equal "pi is about 3.14" (fixed "pi" pi))
(check-equal "e is about 2.72" (fixed "e" 2.718281828))
+19
View File
@@ -0,0 +1,19 @@
;;; STRINGS 6: FORMAT PICKS AND CASE (solution)
(defun tick-tock (n)
(format nil "~[tick~;tock~]" (mod n 2)))
(defun quiet-title (s)
(format nil "~(~a~)" s))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "tick" (tick-tock 0))
(check-equal "tock" (tick-tock 1))
(check-equal "tick" (tick-tock 2))
(check-equal "tock" (tick-tock 5))
(check-equal "grace" (quiet-title "GRACE"))
(check-equal "ada lovelace" (quiet-title "ADA LOVELACE"))
+22
View File
@@ -0,0 +1,22 @@
;;; STRINGS 7: NEWLINES AND COLUMNS (solution)
(defun add-line (s)
(format nil "~&~a" s))
(defun right-column (name width)
(format nil "~v@a" width name))
(defun receipt (item price)
(format nil "~8a~4@a" item price))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "hi" (add-line "hi"))
(check-equal "parrot" (add-line "parrot"))
(check-equal " ada" (right-column "ada" 6))
(check-equal "hi" (right-column "hi" 2))
(check-equal "tea 2" (receipt "tea" 2))
(check-equal "coffee 3" (receipt "coffee" 3))
+25
View File
@@ -0,0 +1,25 @@
;;; LOOPS 4: LOOP'S OTHER CLAUSES (solution)
(defun doublings (n)
(loop repeat n
for x = 1 then (* x 2)
collect x))
(defun cart-until-cap (xs cap)
(loop with total = 0
for p in xs
until (>= (+ total p) cap)
do (setf total (+ total p))
collect p))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(1 2 4 8) (doublings 4))
(check-equal '(1) (doublings 1))
(check-equal '() (doublings 0))
(check-equal '(3 5) (cart-until-cap '(3 5 2 8) 10))
(check-equal '() (cart-until-cap '(20 1) 10))
(check-equal '(1 2) (cart-until-cap '(1 2 3 4) 6))
+24
View File
@@ -0,0 +1,24 @@
;;; LOOPS 5: DO AND DO* (solution)
(defun triangle-do (n)
(do ((i 1 (1+ i))
(total 0 (+ total i)))
((> i n) total)))
(defun factorial-of (n)
(do* ((a 1 (1+ a))
(b 1 (* b a)))
((>= a n) b)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 6 (triangle-do 3))
(check-equal 55 (triangle-do 10))
(check-equal 0 (triangle-do 0))
(check-equal 1 (factorial-of 0))
(check-equal 1 (factorial-of 1))
(check-equal 6 (factorial-of 3))
(check-equal 24 (factorial-of 4))
+35
View File
@@ -0,0 +1,35 @@
;;; LOOPS 6: COUNT, MAXIMIZE, LOOP-FINISH (solution)
(defun count-odds (xs)
(loop for x in xs
count (oddp x)))
(defun widest (xs)
(if (null xs)
nil
(loop for x in xs
maximize x)))
(defun first-sum-over (xs limit)
(loop with total = 0
for x in xs
do (if (> (+ total x) limit)
(loop-finish)
(setf total (+ total x)))
finally (return total)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 3 (count-odds '(1 2 3 4 5)))
(check-equal 0 (count-odds '(2 4)))
(check-equal 0 (count-odds '()))
(check-equal 9 (widest '(3 9 2)))
(check-equal 4 (widest '(4)))
(check-equal nil (widest '()))
(check-equal 5 (first-sum-over '(2 3 4) 8))
(check-equal 9 (first-sum-over '(2 3 4) 100))
(check-equal 0 (first-sum-over '(9 9) 8))
(check-equal 0 (first-sum-over '() 5))
+22
View File
@@ -0,0 +1,22 @@
;;; NUMBERS 4: TYPE DECLARATIONS (solution)
(defun counted-double (n)
(check-type n integer)
(* 2 n))
(defun shrink (n)
(check-type n (integer 0 100))
(- 100 n))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 10 (counted-double 5))
(check-equal 0 (counted-double 0))
(check-error (counted-double "five") 'type-error)
(check-equal 60 (shrink 40))
(check-equal 100 (shrink 0))
(check-error (shrink 200) 'type-error)
(check-error (shrink -3) 'type-error)
+34
View File
@@ -0,0 +1,34 @@
;;; HASH TABLES 4: MAPHASH (solution)
(defparameter *stock*
(let ((table (make-hash-table)))
(setf (gethash 'tea table) 2
(gethash 'coffee table) 3
(gethash 'cocoa table) 4)
table))
(defun stock-total ()
(let ((total 0))
(maphash (lambda (key value)
(incf total value))
*stock*)
total))
(defun keys-of (table)
(let ((keys '()))
(maphash (lambda (key value)
(push key keys))
table)
keys))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 9 (stock-total))
(check (let ((keys (keys-of *stock*)))
(and (= 3 (length keys))
(member 'tea keys)
(member 'coffee keys)
(member 'cocoa keys))))
+31
View File
@@ -0,0 +1,31 @@
;;; PACKAGES 4: FIND-SYMBOL AND INTERN (solution)
(defpackage #:attic
(:use #:common-lisp)
(:export #:heirloom))
(in-package #:attic)
(defun heirloom ()
'grandmas-ring)
(in-package #:common-lisp-user)
(defun attic-lookup (name)
(find-symbol name 'attic))
(defun attic-intern (name)
(intern name 'attic))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(attic:heirloom :external)
(multiple-value-list (attic-lookup "HEIRLOOM")))
(check-equal '(nil nil)
(multiple-value-list (attic-lookup "GHOST")))
(check (attic-intern "SPARE-KEY"))
(check-equal "SPARE-KEY" (symbol-name (attic-intern "SPARE-KEY")))
(check (eq 'attic:heirloom (attic-intern "HEIRLOOM")))
+41
View File
@@ -0,0 +1,41 @@
;;; PACKAGES 5: INTERNALS, NICKNAMES, SHADOWING (solution)
(defpackage #:shop
(:use #:common-lisp)
(:nicknames #:store)
(:export #:price))
(in-package #:shop)
(defun price ()
5)
(defun secret-sale ()
2)
(in-package #:common-lisp-user)
(defun best-deal ()
(shop::secret-sale))
(defpackage #:post-office
(:use #:common-lisp)
(:shadow #:fill))
(in-package #:post-office)
(defun fill (box)
(list 'stuffed box))
(in-package #:common-lisp-user)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 5 (store:price))
(check-equal 2 (best-deal))
(check-equal (list 0 0 0) (fill (list 1 2 3) 0))
(check-equal '(post-office::stuffed crate)
(funcall (find-symbol "FILL" '#:post-office) 'crate))
+26
View File
@@ -0,0 +1,26 @@
;;; CLOS 5: WITH-SLOTS AND WITH-ACCESSORS (solution)
(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y)))
(defun double-point (p)
(with-slots (x y) p
(make-instance 'point :x (* x 2) :y (* y 2))))
(defun shift-x (p)
(with-accessors ((x point-x)) p
(setf x (1+ x)))
p)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (let ((q (double-point (make-instance 'point :x 3 :y 4))))
(and (eql 6 (point-x q)) (eql 8 (point-y q)))))
(check (let ((p (shift-x (make-instance 'point :x 1))))
(eql 2 (point-x p))))
(check (let ((p (shift-x (make-instance 'point :x 10))))
(eql 11 (point-x p))))
+29
View File
@@ -0,0 +1,29 @@
;;; CLOS 6: CALL-NEXT-METHOD AND NEXT-METHOD-P (solution)
(defclass animal () ())
(defclass dog (animal) ())
(defclass cat (animal) ())
(defgeneric announce (a))
(defmethod announce ((a animal))
"I am an animal.")
(defgeneric purr (a))
(defmethod announce ((d dog))
(concatenate 'string (call-next-method) " Woof!"))
(defmethod purr ((c cat))
(if (next-method-p)
(call-next-method)
"Prrr"))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "I am an animal. Woof!" (announce (make-instance 'dog)))
(check-equal "I am an animal." (announce (make-instance 'animal)))
(check-equal "Prrr" (purr (make-instance 'cat)))
+24
View File
@@ -0,0 +1,24 @@
;;; CLOS 7: PRINT-OBJECT AND DEFAULT-INITARGS (solution)
(defstruct candy filling size)
(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y))
(:default-initargs :x 0 :y 0))
(defmethod print-object ((p point) stream)
(print-unreadable-object (p stream :type t)
(format stream "x=~a y=~a" (point-x p) (point-y p))))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 'swirl (candy-filling (make-candy :filling 'swirl)))
(check-equal 0 (point-x (make-instance 'point)))
(check-equal 7 (point-x (make-instance 'point :x 7)))
(check-equal "#<POINT x=3 y=4>"
(with-output-to-string (s)
(princ (make-instance 'point :x 3 :y 4) s)))
+40
View File
@@ -0,0 +1,40 @@
;;; CONDITIONS 5: REPORTS, WARN, SIGNAL (solution)
(define-condition low-battery (condition) ())
(define-condition empty-cart (error)
()
(:report "The cart is empty - nothing to check out."))
(defun checkout (items)
(when (null items)
(error 'empty-cart))
(list 'order items))
(defun battery-check (percent)
(when (< percent 20)
(warn "battery ~a%!" percent))
(list 'battery percent))
(defun check-battery (percent)
(when (<= percent 10)
(signal 'low-battery))
(list 'battery percent))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(order (milk)) (checkout '(milk)))
(check-error (checkout '()) 'empty-cart)
(check-equal "The cart is empty - nothing to check out."
(handler-case (checkout '())
(empty-cart (c) (format nil "~a" c))))
(check-equal '(battery 15) (battery-check 15))
(check-equal '(battery 90) (battery-check 90))
(check-equal :noticed (handler-case (warn "battery low")
(warning () :noticed)))
(check-equal :swapped (handler-case (check-battery 5)
(low-battery () :swapped)))
(check-equal '(battery 20) (check-battery 20))
+22
View File
@@ -0,0 +1,22 @@
;;; CONDITIONS 6: RESTARTS WITH ARGUMENTS, IGNORE-ERRORS (solution)
(defun parse-count (text)
(restart-case (parse-integer text)
(use-value (value)
value)))
(defun maybe-age (text)
(ignore-errors (parse-integer text)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 42 (handler-bind ((parse-error (lambda (c)
(declare (ignore c))
(invoke-restart 'use-value 42))))
(parse-count "abc")))
(check-equal 7 (parse-count "7"))
(check-equal 42 (maybe-age "42"))
(check-equal nil (maybe-age "abc"))
+15
View File
@@ -0,0 +1,15 @@
;;; MACROS 5: READER MACROS, A TASTER (solution)
(defun maybe-reader (stream sub-char num-arg)
(declare (ignore sub-char num-arg))
(list 'maybe (read stream)))
(set-dispatch-macro-character #\# #\? #'maybe-reader)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(maybe red) '#?red)
(check-equal '(maybe (1 2)) '#?(1 2))
+22
View File
@@ -0,0 +1,22 @@
;;; DYNAMIC 4: DEFVAR VS DEFPARAMETER (solution)
(defvar *cache* 10)
(defparameter *config* 10)
(setf *cache* 99)
(setf *config* 99)
(defvar *cache* 10)
(defparameter *config* 10)
(defun current-values ()
(list *cache* *config*))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(99 10) (current-values))
(check-equal 99 *cache*)
(check-equal 10 *config*)
+25
View File
@@ -0,0 +1,25 @@
;;; SEQUENCES 1: FIND, POSITION, COUNT (solution)
(defun index-of (x xs)
(position x xs))
(defun occurrences (x xs)
(count x xs))
(defun find-same (x xs)
(find x xs :test #'=))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 1 (index-of 'b #(a b c)))
(check-equal 2 (index-of 3 '(1 2 3)))
(check-equal nil (index-of 9 '(1 2 3)))
(check-equal 3 (occurrences 'a #(a b a c a)))
(check-equal 2 (occurrences 'b '(a b a b a)))
(check-equal 0 (occurrences 5 '(1 2 3)))
(check-equal 1 (find-same 1.0 '(2 1 3)))
(check-equal 2 (find-same 2.0 '(1 2 3)))
(check-equal nil (find-same 9 '(1 2 3)))
+22
View File
@@ -0,0 +1,22 @@
;;; SEQUENCES 2: REMOVE, REMOVE-IF, SUBSTITUTE (solution)
(defun drop-them (x xs)
(remove x xs))
(defun drop-negatives (xs)
(remove-if #'minusp xs))
(defun swap-them (new old xs)
(substitute new old xs))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(a c) (drop-them 'b '(a b c b)))
(check-equal '(1 3) (drop-them 2 '(2 1 2 3 2)))
(check-equal '(1 0 2) (drop-negatives '(-1 1 0 -2 2)))
(check-equal '(3) (drop-negatives '(-5 -3 3)))
(check-equal '(x 2 x) (swap-them 'x 1 '(1 2 1)))
(check-equal '(a b a) (swap-them 'a 'z '(z b z)))
+25
View File
@@ -0,0 +1,25 @@
;;; SEQUENCES 3: SORT, :KEY, REVERSE (solution)
(defun ascending (xs)
(sort xs #'<))
(defun by-price (alist)
(sort alist #'< :key #'cdr))
(defun back-words (xs)
(mapcar #'reverse xs))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(1 2 3) (ascending (list 3 1 2)))
(check-equal '(-1 0 4) (ascending (list 4 0 -1)))
(check-equal '(3 2 1) (reverse (list 1 2 3)))
(check-equal '((tea . 1) (cola . 2))
(by-price (list (cons 'cola 2) (cons 'tea 1))))
(check-equal '((b . 1) (c . 2) (a . 7))
(by-price (list (cons 'a 7) (cons 'b 1) (cons 'c 2))))
(check-equal '("tac" "esaelp") (back-words '("cat" "please")))
(check-equal '("bba" "aa") (back-words '("abb" "aa")))
+27
View File
@@ -0,0 +1,27 @@
;;; SEQUENCES 4: EVERY, SOME, SEARCH, MERGE (solution)
(defun all-fit (f xs)
(every f xs))
(defun needle-at (needle hay)
(search needle hay))
(defun weave (a b)
(merge 'list a b #'<))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal t (all-fit #'plusp '(1 2 3)))
(check-equal nil (all-fit #'plusp '(-1 2 3)))
(check-equal t (all-fit #'plusp '()))
(check (some #'minusp '(1 -2 3)))
(check-equal 1 (needle-at '(b c) '(a b c d)))
(check-equal 2 (needle-at '(2 3) '(1 5 2 3)))
(check-equal 1 (needle-at "ana" "banana"))
(check-equal nil (needle-at '(9) '(1 2 3)))
(check-equal '(1 2 3 4) (weave (list 1 3) (list 2 4)))
(check-equal '(0 1 4 9) (weave (list 4) (list 0 1 9)))
(check-equal '(2 3) (weave (list 2 3) (list)))
+29
View File
@@ -0,0 +1,29 @@
;;; VECTORS 1: MAKE-ARRAY, AREF, AND #(...) (solution)
(defun slot-one (v)
(aref v 1))
(defun replace-first (v x)
(setf (aref v 0) x)
v)
(defun triple-vector (a b c)
(vector a b c))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 'b (slot-one #(a b c)))
(check-equal 2 (slot-one #(1 2 3)))
(check-equal 'x
(aref (replace-first (make-array 2 :initial-element 'a) 'x)
0))
(check-equal 99
(aref (replace-first (make-array 3 :initial-element 0) 99)
0))
(check (vectorp (triple-vector 'a 'b 'c)))
(check (equalp #(p q r) (triple-vector 'p 'q 'r)))
(check-equal 'q (aref (triple-vector 'p 'q 'r) 1))
(check-equal 3 (length #(a b c)))
+39
View File
@@ -0,0 +1,39 @@
;;; VECTORS 2: FILL POINTERS AND GROWING VECTORS (solution)
(defun blank-row (room)
(make-array room :fill-pointer 0))
(defun triple-push (v x)
(vector-push x v)
(vector-push x v)
(vector-push x v)
v)
(defun growable (a b)
(make-array 2 :initial-contents (list a b) :fill-pointer 2
:adjustable t))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 0 (fill-pointer (blank-row 4)))
(check-equal 0 (vector-push 'z (blank-row 5)))
(check (let ((v (blank-row 3)))
(vector-push 'a v)
(equal 1 (fill-pointer v))))
(check-equal nil (vector-push 'x (make-array 2 :fill-pointer 2)))
(check-error (fill-pointer (make-array 3)))
(check-equal 3
(fill-pointer (triple-push (make-array 3 :fill-pointer 0)
'q)))
(check-equal 'q
(aref (triple-push (make-array 3 :fill-pointer 0) 'q)
2))
(check (let ((v (growable 'a 'b)))
(vector-push-extend 'c v)
(equal 'c (aref v 2))))
(check (let ((v (growable 1 2)))
(vector-push-extend 9 v)
(equal 9 (aref v 2))))
+29
View File
@@ -0,0 +1,29 @@
;;; VECTORS 3: STRINGS ARE VECTORS (solution)
(defun first-letter (w)
(aref w 0))
(defun retag (w c)
(let ((s (make-array (length w) :element-type 'character
:initial-contents w)))
(setf (aref s (- (length s) 1)) c)
s))
(defun letter-run (n c)
(make-array n :element-type 'character :initial-element c))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (vectorp "cat"))
(check-equal #\c (first-letter "cat"))
(check-equal #\a (first-letter "apple"))
(check-equal "cab" (retag "cat" #\b))
(check-equal "suns" (retag "sung" #\s))
(check (let ((w "cat"))
(retag w #\b)
(equal "cat" w)))
(check-equal "aaa" (letter-run 3 #\a))
(check-equal "xx" (letter-run 2 #\x))
+20
View File
@@ -0,0 +1,20 @@
;;; FILES 1: WITH-OPEN-FILE (solution)
(defparameter *note-file* "lisplings-note.txt")
(defun save-note ()
(with-open-file (out *note-file* :direction :output :if-exists :supersede)
(format out "(lisp is lovely)~%")
t))
(defun load-note ()
(with-open-file (in *note-file*)
(read in)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-note))
(check-equal '(lisp is lovely) (load-note))
+30
View File
@@ -0,0 +1,30 @@
;;; FILES 2: READING LINES (solution)
(defparameter *poem-file* "lisplings-poem.txt")
(defun save-poem ()
(with-open-file (out *poem-file* :direction :output :if-exists :supersede)
(format out "(lisp is life)~%")
(format out "lambda forever~%")
(format out "close the parens~%"))
t)
(defun poem-lines ()
(with-open-file (in *poem-file*)
(poem-rest in)))
(defun poem-rest (in)
(let ((line (read-line in nil nil)))
(if line
(cons line (poem-rest in))
nil)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-poem))
(check-equal '("(lisp is life)" "lambda forever" "close the parens")
(poem-lines))
(check-equal 3 (length (poem-lines)))
+22
View File
@@ -0,0 +1,22 @@
;;; FILES 3: PRINTING TO STREAMS (solution)
(defun two-views (x)
(with-output-to-string (s)
(prin1 x s)
(princ " " s)
(princ x s)))
(defun name-tag (name)
(with-output-to-string (s)
(print "name:" s)
(princ name s)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "\"hi\" hi" (two-views "hi"))
(check-equal "7 7" (two-views 7))
(check-equal "
\"name:\" ada" (name-tag "ada"))
+34
View File
@@ -0,0 +1,34 @@
;;; FILES 4: NUMBERS FROM FILES (solution)
(defparameter *count-file* "lisplings-counts.txt")
(defun save-counts ()
(with-open-file (out *count-file* :direction :output :if-exists :supersede)
(format out "10~%")
(format out "20~%")
(format out "5~%"))
t)
(defun sum-counts ()
(with-open-file (in *count-file*)
(sum-rest in)))
(defun sum-rest (in)
(let ((line (read-line in nil nil)))
(if line
(+ (parse-integer line) (sum-rest in))
0)))
(defun first-number (s)
(parse-integer s :junk-allowed t))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-counts))
(check-equal 35 (sum-counts))
(check-equal 12 (first-number "12 apples"))
(check-equal nil (first-number "no digits here"))
(check-error (parse-integer "12 apples") 'parse-error)
+50
View File
@@ -0,0 +1,50 @@
;;; QUIZ 3: THE LUNCH TALLY (solution)
(define-condition not-ordered (error)
((item :initarg :item :reader not-ordered-item))
(:documentation "Signaled when the kitchen is asked for an item nobody ordered."))
(defparameter *orders*
'(soup bread soup bread soup))
(defparameter *menu*
'((soup . 7) (bread . 3) (salad . 5)))
(defun tally-orders (orders)
(let ((table (make-hash-table)))
(dolist (order orders table)
(incf (gethash order table 0)))))
(defun kitchen-count (table item)
(multiple-value-bind (count present) (gethash item table)
(cond (present count)
(t (error 'not-ordered :item item)))))
(defun price-of (menu item)
(let ((pair (assoc item menu)))
(if pair (cdr pair) 0)))
(defun bill-total (orders menu)
(let ((total 0))
(dolist (order orders total)
(incf total (price-of menu order)))))
(defun order-line (items)
(string-downcase (format nil "~{~a~^, ~}" items)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (let ((table (tally-orders *orders*)))
(and (eql 3 (gethash 'soup table))
(eql 2 (gethash 'bread table))
(eql 2 (hash-table-count table)))))
(check-equal 3 (kitchen-count (tally-orders *orders*) 'soup))
(check-error (kitchen-count (tally-orders *orders*) 'pizza) 'not-ordered)
(check-equal 7 (price-of *menu* 'soup))
(check-equal 0 (price-of *menu* 'pizza))
(check-equal 27 (bill-total *orders* *menu*))
(check-equal "soup, bread, salad" (order-line '(soup bread salad)))
(check-equal 0 (hash-table-count (tally-orders '())))