Files
lisplings/info.sexp
T
venomade 4df60ead93 Lisplings: Rustlings-style exercises for Common Lisp
70 exercises in 20 chapters, from first s-expressions to restarts,
macros, CLOS, and a tiny Lisp interpreter. Each exercise ships
broken with a solved twin and a hint. Includes a watch-mode runner,
a plain-CL check harness, a nix flake, and verification tooling.
All plain SBCL, no dependencies.
2026-11-22 03:30:19 +00:00

241 lines
13 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 shown by `lisplings hint <name>'
((: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 can now read and write
Common Lisp, and you know how to find out more: (describe 'foo),
(apropos \"foo\"), and the HyperSpec are your friends.
Found an issue, or want to add an exercise? Improvements welcome!")
(: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 "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)) ...). For the box, a keyword default may use an earlier parameter: (defun make-box (width &key (height width) (depth width)) ...).")
(: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 "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 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 "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 "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 "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 "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 "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 "loops1"
:dir "10_loops"
:hint "DOLIST walks a list: (dolist (x xs) (incf 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 "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 "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 "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 "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 "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 "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 "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 "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 "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."))))