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 @@
|
||||
# 17_macros
|
||||
|
||||
DEFMACRO, backquote templates, hygiene with GENSYM.
|
||||
|
||||
A macro writes code. The backquote family (` , ,@) makes templates
|
||||
cheap, and GENSYM keeps a macro's own variables from capturing
|
||||
yours.
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; MACROS 1: DEFMACRO
|
||||
;;;
|
||||
;;; A macro takes unevaluated CODE and returns the code to run.
|
||||
;;; DEFMACRO defines one; the arguments arrive as source, not
|
||||
;;; values.
|
||||
|
||||
;; TODO: my-when expands (my-when test body...) so the body runs
|
||||
;;; only when TEST is true; the whole form is NIL otherwise.
|
||||
(defmacro my-when (test &body body)
|
||||
`(progn ,test ,@body))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 5 (my-when t 5))
|
||||
(check-equal nil (my-when nil 5))
|
||||
(check-equal :more (my-when t :ok :more))
|
||||
@@ -0,0 +1,24 @@
|
||||
;;; MACROS 2: BACKQUOTE, COMMA, SPLICE
|
||||
;;;
|
||||
;;; ` is a code template. Inside it almost everything is literal,
|
||||
;;; except:
|
||||
;;; ,expr inserts the value of expr
|
||||
;;; ,@expr SPLICES a list into the surrounding list
|
||||
|
||||
;; TODO: (boxed <x>): the symbol BOXED with the VALUE of X inside.
|
||||
(defun wrap-value (x)
|
||||
`(boxed x))
|
||||
|
||||
;; TODO: (add <numbers...>): the symbol ADD with the numbers
|
||||
;;; spliced in.
|
||||
(defun make-add-form (numbers)
|
||||
`(add))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(boxed 42) (wrap-value 42))
|
||||
(check-equal '(add 2 3 4) (make-add-form '(2 3 4)))
|
||||
(check-equal '(add) (make-add-form '()))
|
||||
@@ -0,0 +1,21 @@
|
||||
;;; MACROS 3: GENSYM AND HYGIENE
|
||||
;;;
|
||||
;;; A macro that introduces its own variables can capture the
|
||||
;;; user's variables by accident. GENSYM makes a name that cannot
|
||||
;;; collide, because it never existed before.
|
||||
|
||||
;; TODO: n-plus-five should add 5 to the user's N. The macro's own
|
||||
;;; temp variable must not shadow it - use GENSYM.
|
||||
(defmacro n-plus-five (n)
|
||||
`(let ((n 5))
|
||||
(+ n ,n)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 7 (let ((n 2))
|
||||
(n-plus-five n)))
|
||||
(check-equal 15 (let ((n 10))
|
||||
(n-plus-five n)))
|
||||
@@ -0,0 +1,28 @@
|
||||
;;; MACROS 4: A MACRO OF YOUR OWN
|
||||
;;;
|
||||
;;; MACROEXPAND-1 shows what any macro does with your code - try it
|
||||
;;; in a REPL. Time to write a real one: WHILE runs its body until
|
||||
;;; the test turns NIL.
|
||||
;;;
|
||||
;;; (let ((i 0))
|
||||
;;; (while (< i 3)
|
||||
;;; (incf i))
|
||||
;;; i) => 3
|
||||
|
||||
;; TODO: Define while with LOOP.
|
||||
(defmacro while (test &body body)
|
||||
`(progn ,test))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 3 (let ((i 0))
|
||||
(while (< i 3)
|
||||
(incf i))
|
||||
i))
|
||||
(check-equal '() (let ((acc '()))
|
||||
(while nil
|
||||
(push 1 acc))
|
||||
acc))
|
||||
Reference in new issue
Block a user