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

+5
View File
@@ -0,0 +1,5 @@
# 12_structs
DEFSTRUCT: constructors, readers, predicates, defaults, inheritance.
One form generates a whole toolkit around a record type.
+27
View File
@@ -0,0 +1,27 @@
;;; STRUCTS 1: DEFSTRUCT
;;;
;;; DEFSTRUCT defines a record type and generates, for free:
;;; a constructor (make-person), slot readers (person-name),
;;; and a predicate (personp).
(defstruct person
name
age)
;; TODO: Make Ada: name "Ada", age 36.
(defun make-ada ()
(make-person :name "Ada"))
;; TODO: Ada's age.
(defun ada-age (ada)
(person-name ada))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (let ((ada (make-ada)))
(and (equal "Ada" (person-name ada))
(eql 36 (person-age ada)))))
(check (eql 36 (ada-age (make-ada))))
+29
View File
@@ -0,0 +1,29 @@
;;; STRUCTS 2: DEFAULTS AND PREDICATES
;;;
;;; Slots may have defaults: (name "unnamed"). DEFSTRUCT also
;;; generates a type predicate: spaceship-p.
(defstruct spaceship
(name "unnamed")
(crew-size 1))
;; TODO: A ship with the default name and a crew of 7.
(defun seven-crew ()
(make-spaceship :crew-size 1))
;; TODO: T when X is a spaceship, NIL otherwise. The predicate was
;;; generated for you.
(defun ship-p (x)
t)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (let ((s (seven-crew)))
(and (equal "unnamed" (spaceship-name s))
(eql 7 (spaceship-crew-size s)))))
(check (and (ship-p (make-spaceship))
(not (ship-p "a plain string"))
(not (ship-p 42))))
+27
View File
@@ -0,0 +1,27 @@
;;; STRUCTS 3: MUTATION
;;;
;;; Struct slots are SETF places:
;;; (setf (person-age p) 37)
(defstruct counter
(value 0))
;; TODO: Add ONE to the counter's value. Return the counter.
(defun bump (c)
c)
;; TODO: Set the counter back to zero. Return the counter.
(defun reset-counter (c)
c)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (let ((c (make-counter)))
(bump c)
(eql 1 (counter-value c))))
(check (let ((c (make-counter :value 9)))
(reset-counter c)
(eql 0 (counter-value c))))
+29
View File
@@ -0,0 +1,29 @@
;;; STRUCTS 4: INHERITANCE
;;;
;;; :INCLUDE extends a struct. A student IS a person, with every
;;; person slot, plus a major.
(defstruct person
name
age)
(defstruct (student (:include person))
major)
;; TODO: Make Zed: name "Zed", age 21, studying physics.
(defun make-zed ()
(make-person :name "Zed"))
;; TODO: Zed's major.
(defun zed-major (zed)
(person-name zed))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (let ((zed (make-zed)))
(and (equal "Zed" (student-name zed))
(eql 21 (student-age zed))
(equal "physics" (zed-major zed)))))