378 lines
25 KiB
Common Lisp
378 lines
25 KiB
Common Lisp
;;; Lisplings exercise catalogue.
|
|
;;;
|
|
;;; Read by the lisplings runner. Each exercise is a plist:
|
|
;;; :name the exercise name; file is exercises/<dir>/<name>.lisp
|
|
;;; :dir the chapter directory
|
|
;;; :hint one hint paragraph, or a list of them; watch mode reveals
|
|
;;; one per `h', `lisplings hint <name>' prints them all
|
|
|
|
((:welcome "Welcome to Lisplings!
|
|
|
|
Is this your first time? Don't worry, Lisplings is made for beginners!
|
|
We are going to teach you a lot about Common Lisp, but before we can
|
|
get started, here's how Lisplings works:
|
|
|
|
1. The central idea is small exercises with something wrong or
|
|
missing: the file fails to load, or one of its checks fails.
|
|
Your job is to fix the exercise.
|
|
2. Lisplings watches the current exercise file. Edit it in your
|
|
editor and save; the exercise runs again automatically. When
|
|
everything passes, press n to move on.
|
|
3. Stuck? Press h for a hint. Really stuck? Peek at solutions/.
|
|
4. The REPL is your friend. Run `sbcl' in another terminal and try
|
|
forms interactively. That's the Lisp way!")
|
|
|
|
(:final "We hope you enjoyed Lisplings! You finished every
|
|
exercise, so the lambda is yours:
|
|
|
|
/
|
|
/
|
|
/
|
|
/\\
|
|
/ \\
|
|
/ \\
|
|
|
|
Where to go from here:
|
|
|
|
1. Books. Practical Common Lisp (free at
|
|
http://www.gigamonkeys.com/book/) is the natural companion:
|
|
chapters 4-9 are the ground you just covered. Keep reading
|
|
ch. 10 (numbers, characters, strings), ch. 11 (collections),
|
|
ch. 12-13 (lists, plists, trees), ch. 14 (files and file I/O),
|
|
ch. 16-17 (objects), ch. 19 (conditions and restarts), and
|
|
LOOP for Black Belts. Its practical chapters then build whole
|
|
working programs, a spam filter among them.
|
|
2. Libraries. Quicklisp is how Common Lisp gets them: install it
|
|
once, and (ql:quickload :whatever) fetches any of thousands.
|
|
ASDF (asdf:defsystem) is how Lisp projects organize their
|
|
files into a system.
|
|
3. The REPL, always. (describe 'foo) prints what SBCL knows
|
|
about a name, (apropos \"foo\") lists every symbol containing
|
|
it, and the HyperSpec answers the rest.
|
|
4. Give back. Lisplings is plain Common Lisp and it wants your
|
|
exercises: write the broken file under exercises/<chapter>/,
|
|
the solved twin under solutions/, add a (:name ... :dir ...
|
|
:hint ...) entry to info.sexp, and run dev/verify.sh.
|
|
|
|
Good luck - and keep a REPL open. That's the Lisp way!")
|
|
|
|
(:exercises
|
|
((:name "intro1"
|
|
:dir "00_intro"
|
|
:hint ("This exercise has no errors. Play with the banner, save the file, and press n in the terminal. You might need to press ENTER after typing n."))
|
|
(:name "intro2"
|
|
:dir "00_intro"
|
|
:hint "The format directive ~a expects a value from the arguments after the format string. The string below has no arguments at all...")
|
|
(:name "sexps1"
|
|
:dir "01_sexps"
|
|
:hint "The operator goes first: (+ 10 32). In (- 100 50) the first argument is what is subtracted FROM, so order matters.")
|
|
(:name "sexps2"
|
|
:dir "01_sexps"
|
|
:hint "Nest the sub-expression where its value belongs: (/ 100 (+ 2 3)). For an exact half, drop the 2.0: (/ 1 2) is the rational 1/2, not 0.5.")
|
|
(:name "sexps3"
|
|
:dir "01_sexps"
|
|
:hint "Quote stops evaluation: '(+ 1 2) is the LIST (+ 1 2). The single quote is reader shorthand for (quote x).")
|
|
(:name "sexps4"
|
|
:dir "01_sexps"
|
|
:hint "Reader shorthand: #'add is the function itself where '+ is only its name. #(1 2 3) reads a vector in one piece. #s(point :x 1 :y 2) reads a POINT structure. And #xdeadbeef spells 3735928559 in hex.")
|
|
(:name "functions1"
|
|
:dir "02_functions"
|
|
:hint "DEFUN: (defun name (params) body). The last form of the body is the return value. Twice X is (* 2 x), and addition is (+ a b).")
|
|
(:name "functions2"
|
|
:dir "02_functions"
|
|
:hint "Match the parameter list to the calls: average is called with TWO numbers here. And (- a b) subtracts b from a - check which way round you wrote it.")
|
|
(:name "functions3"
|
|
:dir "02_functions"
|
|
:hint ("&OPTIONAL gives a parameter a default: (defun repeat-word (word &optional (n 2)) ...)."
|
|
"A keyword default may use an earlier parameter: (defun make-box (width &key (height width) (depth width)) ...)."
|
|
"Further reading: CLHS 3.4.1 (ordinary lambda lists) and PCL ch. 5, https://gigamonkeys.com/book/functions.html"))
|
|
(:name "functions4"
|
|
:dir "02_functions"
|
|
:hint "FLOOR returns two values at once: (floor 10 3) is 3 and 1. Pass it straight through. For 125 seconds: (floor seconds 60) is 2 minutes and 5 seconds.")
|
|
(:name "functions5"
|
|
:dir "02_functions"
|
|
:hint "VALUES hands back two answers at once: (values 2 65) is TWO values - a LIST is one value wearing brackets. FLOOR's first value is the dollars and (mod cents 100) the cents. HALF-AND-DOUBLE returns (/ n 2) and (* n 2) together.")
|
|
(:name "variables1"
|
|
:dir "03_variables"
|
|
:hint "DEFPARAMETER gives a global its value: (defparameter *room-capacity* 100). Any capacity from 3 to 199 passes both checks.")
|
|
(:name "variables2"
|
|
:dir "03_variables"
|
|
:hint "The inner LET's initializer evaluates in the OUTER scope, so it can see the outer x: (let ((x (+ x 5))) ...). For wrap-me, introduce a LET of your own with a suitable binding.")
|
|
(:name "variables3"
|
|
:dir "03_variables"
|
|
:hint "LET binds all at once; LET* binds one by one, so B can be (+ A 2) and still see A's value.")
|
|
(:name "variables4"
|
|
:dir "03_variables"
|
|
:hint "Read the current balance first, then SETF the place to the sum: (setf (getf *account* :balance) (+ amount (getf *account* :balance))).")
|
|
(:name "if1"
|
|
:dir "04_conditionals"
|
|
:hint "The condition needs to be a TEST, not the temperature itself: (> temperature 30). Any number - even 0 - is true, so the broken version always answers hot.")
|
|
(:name "if2"
|
|
:dir "04_conditionals"
|
|
:hint "IF takes exactly one then-form and at most one else-form; the fourth part makes it malformed. COND handles many branches: (cond ((zerop n) \"zero\") ((minusp n) \"negative\") (t \"positive\")).")
|
|
(:name "if3"
|
|
:dir "04_conditionals"
|
|
:hint "WHEN runs its body when the test is TRUE - plusp, not minusp. For enter, only an IF with both branches can return :denied when the door is shut.")
|
|
(:name "if4"
|
|
:dir "04_conditionals"
|
|
:hint "OR returns the first non-NIL value: (or a b c). For safe-div, UNLESS can guard the division and OR supplies the fallback: (or (unless (zerop b) (/ a b)) :undefined).")
|
|
(:name "if5"
|
|
:dir "04_conditionals"
|
|
:hint "CASE tests with EQL, so bare symbols work as keys: (case fruit ((apple banana) \"cheap\") ...). Group keys in one list; a (t ...) clause is the fallback. ECASE has no fallback - a miss signals a TYPE-ERROR, which the last check wants.")
|
|
(:name "pred1"
|
|
:dir "05_predicates"
|
|
:hint "Only NIL is false. (not (null thing)) is true for everything else, including 0 and the empty string.")
|
|
(:name "pred2"
|
|
:dir "05_predicates"
|
|
:hint "EQ compares identity: two separately built lists are different objects. EQUAL compares structure. Numbers compare by value with =, and (= 1 1.0) is T.")
|
|
(:name "pred3"
|
|
:dir "05_predicates"
|
|
:hint "Write a COND. Check (NULL V) before the others: (listp nil) is also true, so the :nothing branch must come first.")
|
|
(:name "pred4"
|
|
:dir "05_predicates"
|
|
:hint "EQL demands the same type too: (eql 1 1.0) is NIL. EQUALP is easygoing: numbers compare by value - (equalp 1 1.0) is T - and characters ignore case, so (equalp #\\a #\\A) is T.")
|
|
(:name "lists1"
|
|
:dir "06_lists"
|
|
:hint "CAR takes the first element, CDR takes the rest. To put one element in front, CONS it: (cons x xs) - (list x xs) would nest the list instead.")
|
|
(:name "lists2"
|
|
:dir "06_lists"
|
|
:hint "Quote writes a list down as data: '(a b c). LIST builds one at runtime: (list 1 2 3). APPEND joins lists: (append '(1 2) '(3 4)).")
|
|
(:name "lists3"
|
|
:dir "06_lists"
|
|
:hint "NTH counts from zero. LAST returns the last CONS cell - take its CAR for the element. BUTLAST drops the last element.")
|
|
(:name "lists4"
|
|
:dir "06_lists"
|
|
:hint "PUSH and POP work on a variable holding a list: (push x ys) conses x on. Two pushes end up in reverse order - push 0 first, then 1.")
|
|
(:name "lists5"
|
|
:dir "06_lists"
|
|
:hint "ASSOC finds a pair by key: (assoc item *menu*) returns the pair; its CDR is the price. PUSH a new (cons item cost) pair onto the alist.")
|
|
(:name "lists6"
|
|
:dir "06_lists"
|
|
:hint "DESTRUCTURING-BIND is LET for list-shaped data: (destructuring-bind (w h d) dims (* w h d)). The dotted template (x . rest) splits head from tail. Two answers at once ride on (values x rest) - the check gathers them with MULTIPLE-VALUE-LIST.")
|
|
(:name "vectors1"
|
|
:dir "20_vectors"
|
|
:hint "Slots are numbered from 0: (aref v 1) is the second element. MAKE-ARRAY builds a row of a given size, VECTOR one from its arguments: (vector a b c) => #(A B C). SETF on AREF writes a slot.")
|
|
(:name "vectors2"
|
|
:dir "20_vectors"
|
|
:hint "A fill pointer counts the slots in use: (make-array 5 :fill-pointer 0) has room for five. VECTOR-PUSH stores past it and returns the index, or NIL when the row is full; VECTOR-PUSH-EXTEND grows the vector, so make it :adjustable.")
|
|
(:name "vectors3"
|
|
:dir "20_vectors"
|
|
:hint "A string is a vector of characters: (aref \"cat\" 0) returns the first one. MAKE-ARRAY with :element-type 'character builds strings. Before changing a string you were handed, build your own copy with MAKE-ARRAY :initial-contents.")
|
|
(:name "recurs1"
|
|
:dir "07_recursion"
|
|
:hint "The base case must return 0, not NIL - (+ 3 nil) is a type error. Add the head to the recursive sum of the rest.")
|
|
(:name "recurs2"
|
|
:dir "07_recursion"
|
|
:hint "The recursive call handles a list one shorter. Add 1 for the element you just consumed: (+ 1 (my-length (cdr xs))).")
|
|
(:name "recurs3"
|
|
:dir "07_recursion"
|
|
:hint "Follow the MEMBER convention: return the tail of the list starting at the match, or NIL. Test with (equal x (car xs)), else recurse on (cdr xs).")
|
|
(:name "recurs4"
|
|
:dir "07_recursion"
|
|
:hint "A tree has two parts: (car tree) and (cdr tree). Count the leaves in BOTH and add the results.")
|
|
(:name "recurs5"
|
|
:dir "07_recursion"
|
|
:hint "LABELS defines local functions that can call each other: (labels ((even-step (k) ... (odd-step (1- k))) (odd-step (k) ... (even-step (1- k)))) (even-step n)). Each step takes one off with (1- k) and hands the question to the other; zero answers T for even, NIL for odd.")
|
|
(:name "func1"
|
|
:dir "08_functional"
|
|
:hint "FUNCALL calls a function value: (funcall f 10). LAMBDA makes function values: (lambda (x) (* 2 x)).")
|
|
(:name "func2"
|
|
:dir "08_functional"
|
|
:hint "MAPCAR applies a function to each element: (mapcar (lambda (x) (* 2 x)) xs). STRING-UPCASE is a function too: #'string-upcase.")
|
|
(:name "func3"
|
|
:dir "08_functional"
|
|
:hint "APPLY spreads a list into arguments: (apply #'+ xs) is (+ 1 2 3) for (1 2 3). FUNCALL keeps them spelled out.")
|
|
(:name "func4"
|
|
:dir "08_functional"
|
|
:hint "REDUCE folds: (reduce #'max xs) for the biggest, (reduce #'* xs :initial-value 1) for the product.")
|
|
(:name "sequences1"
|
|
:dir "19_sequences"
|
|
:hint "FIND returns the element, POSITION its index, COUNT the matches: (position 'b '(a b c)) => 1. :test decides what matches; the default treats 1 and 1.0 as different, so use (find x xs :test #'=).")
|
|
(:name "sequences2"
|
|
:dir "19_sequences"
|
|
:hint "REMOVE drops matches: (remove x xs), mind the argument order. REMOVE-IF drops what its predicate accepts. SUBSTITUTE reads (NEW OLD SEQUENCE).")
|
|
(:name "sequences3"
|
|
:dir "19_sequences"
|
|
:hint "SORT takes the comparison first: (sort (list 3 1 2) #'<). With :key it compares one part of each element: (sort pairs #'< :key #'cdr). REVERSE returns a backwards copy - MAPCAR it over each word.")
|
|
(:name "sequences4"
|
|
:dir "19_sequences"
|
|
:hint "SEARCH finds a whole subsequence, not one element: (search '(b c) '(a b c d)) => 1. MERGE interleaves two already-sorted sequences: (merge 'list a b #'<). EVERY is true of the empty list; SOME is not.")
|
|
(:name "quiz1"
|
|
:dir "quizzes"
|
|
:hint ("APPEND makes a new book with the contact at the end."
|
|
"DOLIST + STRING-EQUAL + GETF finds by name; FORMAT makes the card."))
|
|
(:name "str1"
|
|
:dir "09_strings"
|
|
:hint "FORMAT's first argument NIL returns a string. ~a prints a value; ~10a pads it to ten columns.")
|
|
(:name "str2"
|
|
:dir "09_strings"
|
|
:hint "SUBSEQ takes start and end: (subseq s 0 3). CONCATENATE joins strings. SEARCH is case-sensitive - search for lowercase \"lisp\".")
|
|
(:name "str3"
|
|
:dir "09_strings"
|
|
:hint "CONCATENATE joins: (concatenate 'string first \" \" last). STRING-EQUAL compares ignoring case; EQ on strings is unreliable.")
|
|
(:name "str4"
|
|
:dir "09_strings"
|
|
:hint "FORMAT iterates with ~{ ... ~}, and ~^ skips the separator at the last element: (format nil \"~{~a~^, ~}\" xs).")
|
|
(:name "str5"
|
|
:dir "09_strings"
|
|
:hint "A colon modifier makes ~d group with commas: (format nil \"~:d widgets\" 1234567). A number after a comma fixes ~f's decimals: ~,2f keeps two - (format nil \"~a is about ~,2f\" label n) rounds pi to 3.14.")
|
|
(:name "str6"
|
|
:dir "09_strings"
|
|
:hint "~[ ~; ~] picks a branch by number: pass (mod n 2), with tick before the ~; and tock after. ~( ... ~) downcases what it wraps: (format nil \"~(~a~)\" s).")
|
|
(:name "str7"
|
|
:dir "09_strings"
|
|
:hint "~& only breaks the line when there is one to break: a fresh string gets no leading newline, where ~% always adds one. ~v@a takes the width from the arguments and right-justifies. Two fixed columns make the receipt: (format nil \"~8a~4@a\" item price).")
|
|
(:name "files1"
|
|
:dir "21_files"
|
|
:hint ":direction :output is what opens the file for writing - without it WITH-OPEN-FILE opens for INPUT and the write fails. :if-exists :supersede starts the file fresh every save. End SAVE-NOTE's body with T so the check sees a true value. To read back, open the file again and (READ in): READ takes the stream, not the file name.")
|
|
(:name "files2"
|
|
:dir "21_files"
|
|
:hint "READ-LINE returns the next line; (read-line in nil nil) hands you NIL instead of an error at the end. Then it is the recursion chapter's shape: NIL is the base case, (cons line (poem-rest in)) the step. No LOOP needed.")
|
|
(:name "files3"
|
|
:dir "21_files"
|
|
:hint "PRIN1 quotes strings and PRINC does not: (prin1 x s), then a (princ \" \" s), then (princ x s). PRINT is PRIN1 with a newline before and a space after - one call does the label line and its spacing.")
|
|
(:name "files4"
|
|
:dir "21_files"
|
|
:hint "The same shape as poem-rest: (read-line in nil nil) for NIL at the end, (parse-integer line) to turn each line into a number to add. For the junk: PARSE-INTEGER with :JUNK-ALLOWED T reads the leading number and answers NIL when there is none.")
|
|
(:name "loops1"
|
|
:dir "10_loops"
|
|
:hint "DOLIST walks a list: (dolist (x xs) (setf total (+ total x))). DOTIMES counts from 0; PUSH inside it builds the countdown naturally.")
|
|
(:name "loops2"
|
|
:dir "10_loops"
|
|
:hint "COLLECT gathers into a list, SUM adds: (loop for i from 1 to n sum i).")
|
|
(:name "loops3"
|
|
:dir "10_loops"
|
|
:hint "WHEN filters inside LOOP: (loop for x in xs when (evenp x) collect x). THEREIS returns the first non-NIL value and stops: (thereis (if (> x 100) x)).")
|
|
(:name "loops4"
|
|
:dir "10_loops"
|
|
:hint "A FOR with THEN steps a value: (loop repeat n for x = 1 then (* x 2) collect x). For the cart keep the running total with WITH: (loop with total = 0 for p in xs until (>= (+ total p) cap) do (setf total (+ total p)) collect p) - the UNTIL watches the total, not the price.")
|
|
(:name "loops5"
|
|
:dir "10_loops"
|
|
:hint "DO steps every variable with the OLD values, like LET - so TOTAL's step must be (+ total i). DO* steps one at a time like LET*, and B's step (* b a) then sees A's NEW value - swap the factorial's DO for DO* and it multiplies the right pairs.")
|
|
(:name "loops6"
|
|
:dir "10_loops"
|
|
:hint "COUNT wants a test: (loop for x in xs count (oddp x)). MAXIMIZE tracks the biggest: (loop for x in xs maximize x) - but what an empty run answers is implementation-dependent (0 in SBCL), so WIDEST must answer NIL itself when XS is empty. For the wallet LOOP-FINISH bails out before the total goes over: (if (> (+ total x) limit) (loop-finish) (setf total (+ total x))), and FINALLY returns the total either way.")
|
|
(:name "num1"
|
|
:dir "11_numbers"
|
|
:hint "Drop the 2.0: (expt 2 100) and (/ 7 2) stay exact when all arguments are integers.")
|
|
(:name "num2"
|
|
:dir "11_numbers"
|
|
:hint "FLOOR rounds toward minus infinity: (* 5 (floor x 5)). MOD follows FLOOR's sign, so (mod -9 5) is 1.")
|
|
(:name "num3"
|
|
:dir "11_numbers"
|
|
:hint "Clamp with both bounds: (min high (max low x)). Distance is (abs (- a b)). INCF and DECF step a variable in place.")
|
|
(:name "num4"
|
|
:dir "11_numbers"
|
|
:hint "CHECK-TYPE is the live gate: (check-type n integer) signals a TYPE-ERROR when N is not one - the checks count on it. Compound types narrow the gate: (check-type n (integer 0 100)) lets in only whole numbers from 0 to 100.")
|
|
(:name "structs1"
|
|
:dir "12_structs"
|
|
:hint "The constructor takes a keyword for every slot: (make-person :name \"Ada\" :age 36). The reader is (person-age ada).")
|
|
(:name "structs2"
|
|
:dir "12_structs"
|
|
:hint "Leave :name out and the default applies. DEFSTRUCT generated the predicate too: (spaceship-p x).")
|
|
(:name "structs3"
|
|
:dir "12_structs"
|
|
:hint "Struct slots are SETF places: (setf (counter-value c) 0). INCF works on any place.")
|
|
(:name "structs4"
|
|
:dir "12_structs"
|
|
:hint ":INCLUDE inherits: make-student accepts every person slot plus :major. The reader for the new slot is student-major.")
|
|
(:name "hash1"
|
|
:dir "13_hash_tables"
|
|
:hint "SETF on a GETHASH place stores: (setf (gethash 'ada table) 36). And GETHASH takes the table as its SECOND argument.")
|
|
(:name "hash2"
|
|
:dir "13_hash_tables"
|
|
:hint "GETHASH returns two values; NTH-VALUE 1 picks the presence flag. MULTIPLE-VALUE-BIND binds both at once.")
|
|
(:name "hash3"
|
|
:dir "13_hash_tables"
|
|
:hint "GETHASH takes a default: (gethash x table 0) counts missing keys as 0, so (incf (gethash x table 0)) tallies in one line.")
|
|
(:name "hash4"
|
|
:dir "13_hash_tables"
|
|
:hint "MAPHASH hands each pair to a function of KEY and VALUE - mind which is which. Total the stock by (incf total value). Collect keys with LET and PUSH inside the lambda: (maphash (lambda (key value) (push key keys)) table). The walk order is undefined, so the check only asks about length and membership.")
|
|
(:name "pkg1"
|
|
:dir "14_packages"
|
|
:hint "The function now lives in the SHAPES package; the body is just (* s s). The package form above already exported AREA so the checks can reach it.")
|
|
(:name "pkg2"
|
|
:dir "14_packages"
|
|
:hint "The check calls (kitchen:make-soup) with a single colon, which only sees EXPORTED symbols. Add (:export #:make-soup) to the defpackage.")
|
|
(:name "pkg3"
|
|
:dir "14_packages"
|
|
:hint "STUDIO uses SHAPES, so AREA is right there: (+ (area a) (area b)).")
|
|
(:name "pkg4"
|
|
:dir "14_packages"
|
|
:hint "Name the package: (find-symbol name 'attic) - with no package argument it searches *package* and finds nothing. INTERN makes-or-finds: (intern name 'attic) answers with the symbol either way, so (symbol-name (attic-intern \"SPARE-KEY\")) is \"SPARE-KEY\".")
|
|
(:name "pkg5"
|
|
:dir "14_packages"
|
|
:hint "The double colon reaches internals: shop::secret-sale. Add (:shadow #:fill) to the defpackage: POST-OFFICE claims FILL as its own symbol up front, and the DEFUN below defines that one. Without the claim, plain FILL inside POST-OFFICE reads as COMMON-LISP's, and SBCL's package lock stops the file from loading. CL:FILL itself stays untouched - the third check still calls it from CL-USER. The nickname STORE reaches SHOP the single-colon way.")
|
|
(:name "clos1"
|
|
:dir "15_clos"
|
|
:hint "MAKE-INSTANCE with initargs: (make-instance 'dog :name \"Rex\" :tricks '(\"sit\" \"roll-over\")). Initargs are keywords named after the slots.")
|
|
(:name "clos2"
|
|
:dir "15_clos"
|
|
:hint "Write a DEFMETHOD for the base class ANIMAL. Any other animal hits the least specific applicable method.")
|
|
(:name "clos3"
|
|
:dir "15_clos"
|
|
:hint "An :AFTER method just does its job: (push (list 'deposited amount) (account-log a)).")
|
|
(:name "clos4"
|
|
:dir "15_clos"
|
|
:hint "The EQL-specialized method returns \"a free muffin too\" when CUSTOMER is exactly 'vip.")
|
|
(:name "clos5"
|
|
:dir "15_clos"
|
|
:hint "WITH-SLOTS turns slots into plain variables: (with-slots (x y) p (make-instance 'point :x (* x 2) :y (* y 2))). WITH-ACCESSORS names them by accessor: (with-accessors ((x point-x)) p (setf x (1+ x)) p) - SETF goes through the accessor.")
|
|
(:name "clos6"
|
|
:dir "15_clos"
|
|
:hint "CALL-NEXT-METHOD runs the method one step up and you build on its answer: (concatenate 'string (call-next-method) \" Woof!\"). With no method above, CALL-NEXT-METHOD itself errors - so ask first: (if (next-method-p) (call-next-method) \"Prrr\").")
|
|
(:name "clos7"
|
|
:dir "15_clos"
|
|
:hint "Class options ride after the slots list: (:default-initargs :x 0 :y 0) feeds X and Y their defaults, and :x 7 still overrides. To print nicely, define PRINT-OBJECT: (print-unreadable-object (p stream :type t) (format stream \"x=~a y=~a\" (point-x p) (point-y p))) - WITH-OUTPUT-TO-STRING plus PRINC lets the check watch.")
|
|
(:name "cond1"
|
|
:dir "16_conditions"
|
|
:hint "Wrap the call in HANDLER-CASE with a PARSE-ERROR clause that returns :not-a-number.")
|
|
(:name "cond2"
|
|
:dir "16_conditions"
|
|
:hint "UNWIND-PROTECT runs its body, then ALWAYS runs the cleanup form: (unwind-protect (progn (setf *lid* :open) (funcall f)) (setf *lid* :closed)).")
|
|
(:name "cond3"
|
|
:dir "16_conditions"
|
|
:hint "EAT signals (error 'empty-plate :what \"soup\") when XS is NIL. CATCH-EMPTY handles it with HANDLER-CASE and reads the slot with EMPTY-PLATE-WHAT.")
|
|
(:name "cond4"
|
|
:dir "16_conditions"
|
|
:hint "SAFE-CAR wraps its error in RESTART-CASE, offering (use-nil () nil). CAR-WITH-DEFAULT uses HANDLER-BIND and INVOKE-RESTART 'USE-NIL.")
|
|
(:name "cond5"
|
|
:dir "16_conditions"
|
|
:hint "The :REPORT option gives a condition its text: (:report \"The cart is empty - nothing to check out.\") - PRINC of the condition prints it, so the handler-case formats C with ~a. WARN does not unwind: after the warning the body carries on. SIGNAL is quiet the same way - your low-battery handler-case does the unwinding.")
|
|
(:name "cond6"
|
|
:dir "16_conditions"
|
|
:hint "Give the caller a way out: (restart-case (parse-integer text) (use-value (value) value)) - the handler upstairs then (invoke-restart 'use-value 42) and your restart answers with it. IGNORE-ERRORS is the quick cushion: (ignore-errors (parse-integer text)) is NIL when the parse goes wrong.")
|
|
(:name "quiz3"
|
|
:dir "quizzes"
|
|
:hint "Tally with DOLIST and (incf (gethash order table 0)); kitchen-count wants MULTIPLE-VALUE-BIND and (error 'not-ordered ...); a price is (cdr (assoc item menu)); the line is one FORMAT ~{ ~^ join, lowercased with STRING-DOWNCASE.")
|
|
(:name "macro1"
|
|
:dir "17_macros"
|
|
:hint "Return the code that should run: `(if ,test (progn ,@body)). MACROEXPAND-1 in a REPL shows what you built.")
|
|
(:name "macro2"
|
|
:dir "17_macros"
|
|
:hint "A comma inserts one value: `(boxed ,x). A comma-at SPLICES a list in: `(add ,@numbers).")
|
|
(:name "macro3"
|
|
:dir "17_macros"
|
|
:hint "Bind (gensym) in the macro body and use it instead of N, so the user's variable is never shadowed: `(let ((,temp 5)) (+ ,n ,temp)).")
|
|
(:name "macro4"
|
|
:dir "17_macros"
|
|
:hint "LOOP does the work: `(loop while ,test do ,@body).")
|
|
(:name "macro5"
|
|
:dir "17_macros"
|
|
:hint "The reader function returns the object the text becomes: (list 'maybe (read stream)) wraps whatever follows the #?. With (read stream) alone, #?red reads as plain RED - the registration works, the wrapping is the fix.")
|
|
(:name "dyn1"
|
|
:dir "18_dynamic"
|
|
:hint "INCF the captured variable and return the result: (lambda () (incf count)). Each closure has its own COUNT.")
|
|
(:name "dyn2"
|
|
:dir "18_dynamic"
|
|
:hint "LET on a special variable rebinds it dynamically and restores it: (let ((*depth* n)) (funcall f)).")
|
|
(:name "dyn3"
|
|
:dir "18_dynamic"
|
|
:hint "MULTIPLE-VALUE-BIND on GETHASH: if PRESENT return the value, otherwise compute, SETF the table, return. The second call must not touch SLOW-FIB again.")
|
|
(:name "dyn4"
|
|
:dir "18_dynamic"
|
|
:hint "The second DEFVAR kept quiet - *cache* stayed 99 - while the second DEFPARAMETER rebound *config* back to 10. CURRENT-VALUES reads the two variables: (list *cache* *config*).")
|
|
(:name "quiz2"
|
|
:dir "quizzes"
|
|
:hint "A COND over three shapes: numbers, quote forms, and operator forms. MEMBER (car expr) '(+ - *) recognizes the operators; APPLY + MAPCAR evaluates the arguments."))))
|