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.
This commit is contained in:
venomade committed 2026-11-22 03:30:19 +00:00
commit 4df60ead93
172 files changed
+4403

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
# 06_lists
CONS cells, CAR and CDR, list building, association lists.
A list is a chain of pairs ending in NIL. Quote writes lists as data;
LIST and CONS build them at runtime. Association lists map keys to
values with nothing but pairs.
+29
View File
@@ -0,0 +1,29 @@
;;; LISTS 1: CONS, CAR, CDR
;;;
;;; A list is a chain of CONS cells: (cons 1 (cons 2 nil)) is (1 2).
;;; CAR takes the first element; CDR takes the rest.
;;; (car '(1 2 3)) => 1
;;; (cdr '(1 2 3)) => (2 3)
;;; (cons 0 '(1 2)) => (0 1 2)
;; TODO: The first element of XS.
(defun first-of (xs)
(cdr xs))
;; TODO: The list without its first element.
(defun rest-of (xs)
(car xs))
;; TODO: A new list with X in front of XS.
(defun prepend (x xs)
(list x xs))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 1 (first-of '(1 2 3)))
(check-equal '(2 3) (rest-of '(1 2 3)))
(check-equal '(0 1 2) (prepend 0 '(1 2)))
(check-equal '(a) (prepend 'a nil))
+26
View File
@@ -0,0 +1,26 @@
;;; LISTS 2: LIST, QUOTE, AND APPEND
;;;
;;; LIST builds a fresh list at runtime: (list 1 2 3).
;;; QUOTE writes one down as data: '(1 2 3).
;;; APPEND glues lists together: (append '(1 2) '(3 4)) => (1 2 3 4).
;; TODO: The list (a b c), written with a single quote.
(defun abc ()
(list 'a 'b))
;; TODO: The list (1 2 3), built by LIST.
(defun one-two-three ()
(list 1 '(2 3)))
;; TODO: The list (1 2 3 4): two lists joined with APPEND.
(defun joined ()
(cons '(1 2) '(3 4)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(a b c) (abc))
(check-equal '(1 2 3) (one-two-three))
(check-equal '(1 2 3 4) (joined))
+27
View File
@@ -0,0 +1,27 @@
;;; LISTS 3: ELEMENTS BY POSITION
;;;
;;; (nth 0 '(a b c)) => a ; counting starts at zero
;;; (last '(1 2 3)) => (3) ; the LAST CONS, not the element
;;; (butlast '(1 2 3)) => (1 2) ; everything but the last element
;; TODO: The first element of XS.
(defun my-first (xs)
(nth 1 xs))
;; TODO: The last ELEMENT of XS (careful with LAST).
(defun my-last (xs)
(last xs))
;; TODO: Everything except the last element.
(defun drop-last (xs)
(cdr xs))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 'a (my-first '(a b c)))
(check-equal 'c (my-last '(a b c)))
(check-equal '(a b) (drop-last '(a b c)))
(check-equal nil (drop-last '(a)))
+25
View File
@@ -0,0 +1,25 @@
;;; LISTS 4: PUSH AND POP
;;;
;;; PUSH and POP work on a variable holding a list:
;;; (push x ys) is (setf ys (cons x ys))
;;; (pop ys) removes and returns the first element
;; TODO: Push 0 onto XS, then push 1 onto that: the result begins
;;; with (1 0 ...).
(defun push-two (xs)
(let ((ys xs))
(push 0 ys)
ys))
;; TODO: Return XS without its first two elements, using POP.
(defun skip-two (xs)
(let ((ys xs))
ys))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(1 0 a b) (push-two '(a b)))
(check-equal '(c d) (skip-two '(a b c d)))
+29
View File
@@ -0,0 +1,29 @@
;;; LISTS 5: ASSOCIATION LISTS
;;;
;;; An alist maps keys to values with a list of (key . value) pairs:
;;; (assoc 'a '((a . 1) (b . 2))) => (a . 1)
;;; (cdr (assoc 'a ...)) => 1
(defparameter *menu*
'((coffee . 3) (tea . 2) (cocoa . 4)))
;; TODO: The price of a menu item; NIL when it is not served.
(defun price (item)
(car (assoc item *menu*)))
;; TODO: Add a new (ITEM . COST) pair to the menu, in front, and
;;; return the menu.
(defun add-item (item cost)
*menu*)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 3 (price 'coffee))
(check-equal 2 (price 'tea))
(check-equal nil (price 'chai))
(check (let ((menu (add-item 'chai 5)))
(and (eql 5 (price 'chai))
(eql 3 (price 'coffee)))))