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:
commit
4df60ead93
172 files changed
+4403
No files matched your search
@@ -0,0 +1,7 @@
|
||||
# 15_clos
|
||||
|
||||
Classes, instances, generic dispatch, and method combination.
|
||||
|
||||
DEFGENERIC names an operation; DEFMETHOD implements it, once per
|
||||
class. :BEFORE and :AFTER methods combine around the primary one,
|
||||
and (EQL x) specializers dispatch on a single value.
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; CLOS 1: CLASSES AND INSTANCES
|
||||
;;;
|
||||
;;; DEFCLASS defines a class; slots take :INITARG keywords to fill
|
||||
;;; them and :ACCESSOR names to read them.
|
||||
;;; (make-instance 'dog :name "Rex")
|
||||
|
||||
(defclass dog ()
|
||||
((name :initarg :name :accessor dog-name)
|
||||
(tricks :initarg :tricks :accessor dog-tricks :initform '())))
|
||||
|
||||
;; TODO: A dog named "Rex" who knows "sit" and "roll-over".
|
||||
(defun make-rex ()
|
||||
nil)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((rex (make-rex)))
|
||||
(and (string= "Rex" (dog-name rex))
|
||||
(equal '("sit" "roll-over") (dog-tricks rex)))))
|
||||
@@ -0,0 +1,27 @@
|
||||
;;; CLOS 2: GENERIC FUNCTIONS
|
||||
;;;
|
||||
;;; DEFGENERIC names an operation; DEFMETHOD implements it, once
|
||||
;;; per class. The class of each argument picks the method that
|
||||
;;; runs.
|
||||
|
||||
(defclass animal () ())
|
||||
(defclass dog (animal) ())
|
||||
(defclass cat (animal) ())
|
||||
|
||||
(defgeneric speak (a))
|
||||
|
||||
(defmethod speak ((a dog)) "woof")
|
||||
(defmethod speak ((a cat)) "meow")
|
||||
|
||||
;; TODO: Any other animal says "...".
|
||||
(defmethod speak ((a animal))
|
||||
"grrr")
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "woof" (speak (make-instance 'dog)))
|
||||
(check-equal "meow" (speak (make-instance 'cat)))
|
||||
(check-equal "..." (speak (make-instance 'animal)))
|
||||
@@ -0,0 +1,31 @@
|
||||
;;; CLOS 3: COMBINING METHODS
|
||||
;;;
|
||||
;;; :BEFORE runs first, the primary method runs, :AFTER runs last.
|
||||
;;; Every applicable method takes its turn.
|
||||
|
||||
(defclass account ()
|
||||
((log :initform '() :accessor account-log)
|
||||
(balance :initform 0 :accessor account-balance)))
|
||||
|
||||
(defgeneric deposit (account amount))
|
||||
|
||||
(defmethod deposit ((a account) amount)
|
||||
(incf (account-balance a) amount))
|
||||
|
||||
(defmethod deposit :before ((a account) amount)
|
||||
(push (list 'depositing amount) (account-log a)))
|
||||
|
||||
;; TODO: An :AFTER method that logs (list 'deposited amount).
|
||||
(defmethod deposit :after ((a account) amount)
|
||||
(account-log a))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((a (make-instance 'account)))
|
||||
(deposit a 50)
|
||||
(and (eql 50 (account-balance a))
|
||||
(equal '((deposited 50) (depositing 50))
|
||||
(account-log a)))))
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; CLOS 4: DISPATCH ON A VALUE
|
||||
;;;
|
||||
;;; An (EQL x) specializer picks a method for one specific value.
|
||||
;;; It works for numbers, characters and symbols - values that are
|
||||
;;; their own identity.
|
||||
|
||||
(defgeneric serve (customer))
|
||||
|
||||
(defmethod serve (customer)
|
||||
"one espresso")
|
||||
|
||||
;; TODO: The customer 'vip gets a muffin too.
|
||||
(defmethod serve ((customer (eql 'vip)))
|
||||
"espresso")
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "a free muffin too" (serve 'vip))
|
||||
(check-equal "one espresso" (serve 'ada))
|
||||
Reference in new issue
Block a user