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,6 @@
|
||||
# 03_variables
|
||||
|
||||
DEFPARAMETER, LET and LET*, shadowing, and SETF on places.
|
||||
|
||||
Globals conventionally wear earmuffs: `*room-capacity*`. SETF assigns
|
||||
to any place, from plain variables to list positions and properties.
|
||||
@@ -0,0 +1,18 @@
|
||||
;;; VARIABLES 1: GIVING A NAME TO A VALUE
|
||||
;;;
|
||||
;;; DEFPARAMETER introduces a global variable. By convention globals
|
||||
;;; wear earmuffs: *room-capacity*.
|
||||
|
||||
;; TODO: The function below needs *room-capacity* to exist. Give it a
|
||||
;;; value that makes 200 visitors a full room, but 3 visitors not.
|
||||
|
||||
(defun room-full-p (attendees)
|
||||
(> attendees *room-capacity*))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (room-full-p 200))
|
||||
(check (not (room-full-p 3)))
|
||||
@@ -0,0 +1,24 @@
|
||||
;;; VARIABLES 2: LET AND SHADOWING
|
||||
;;;
|
||||
;;; LET creates local variables that exist only inside it. An inner
|
||||
;;; LET may reuse a name: the new binding shadows the old one, and
|
||||
;;; the initializer still evaluates in the outer scope.
|
||||
|
||||
;; TODO: The inner X should be the outer X plus 5 - make it so,
|
||||
;;; without changing either number.
|
||||
(defun shadow-add ()
|
||||
(let ((x 15))
|
||||
(let ((x 15))
|
||||
x)))
|
||||
|
||||
;; TODO: Make wrap-me return 20 by introducing a local variable.
|
||||
(defun wrap-me ()
|
||||
(+ x 5))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 20 (shadow-add))
|
||||
(check-equal 20 (wrap-me))
|
||||
@@ -0,0 +1,18 @@
|
||||
;;; VARIABLES 3: LET AND LET*
|
||||
;;;
|
||||
;;; LET binds everything at once: no initializer sees the others.
|
||||
;;; LET* binds one at a time: each may use the ones before it.
|
||||
|
||||
;; TODO: A should be 3, B should be A plus 2, and the function
|
||||
;;; returns A times B.
|
||||
(defun area-ish ()
|
||||
(let ((a 3)
|
||||
(b (+ a 2)))
|
||||
(* a b)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 15 (area-ish))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; VARIABLES 4: SETF ON PLACES
|
||||
;;;
|
||||
;;; SETF assigns to any "place": a variable, a list element, a
|
||||
;;; property. The general shape is (setf PLACE new-value).
|
||||
|
||||
(defparameter *account* (list :owner "Ada" :balance 100))
|
||||
|
||||
;; TODO: Deposit should ADD the amount to the balance, not replace
|
||||
;;; it.
|
||||
(defun deposit (amount)
|
||||
(setf (getf *account* :balance) amount)
|
||||
*account*)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(:owner "Ada" :balance 150) (deposit 50))
|
||||
(check-equal '(:owner "Ada" :balance 170) (deposit 20))
|
||||
Reference in new issue
Block a user