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 @@
|
||||
# 04_conditionals
|
||||
|
||||
IF, COND, WHEN, UNLESS, and the value-returning AND and OR.
|
||||
|
||||
NIL is the only false value. Zero and the empty string are true, and
|
||||
the empty list IS nil - watch for that one.
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; CONDITIONALS 1: IF
|
||||
;;;
|
||||
;;; (if condition then-form else-form)
|
||||
;;;
|
||||
;;; Exactly one then, at most one else. And the golden rule: NIL is
|
||||
;;; the only false value. Zero, the empty string, everything else:
|
||||
;;; all TRUE.
|
||||
|
||||
;; TODO: "hot" above 30 degrees, "pleasant" otherwise.
|
||||
(defun classify (temperature)
|
||||
(if temperature
|
||||
"hot"
|
||||
"pleasant"))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "hot" (classify 35))
|
||||
(check-equal "pleasant" (classify 20))
|
||||
(check-equal "hot" (classify 31))
|
||||
@@ -0,0 +1,27 @@
|
||||
;;; CONDITIONALS 2: COND
|
||||
;;;
|
||||
;;; IF cannot chain: it takes one test, one then, one else. For many
|
||||
;;; branches there is COND:
|
||||
;;;
|
||||
;;; (cond ((test-1) result-1)
|
||||
;;; ((test-2) result-2)
|
||||
;;; (t fallback))
|
||||
|
||||
;; TODO: describe-number should answer "zero", "negative" or
|
||||
;;; "positive". The IF below is malformed; rewrite with COND.
|
||||
(defun describe-number (n)
|
||||
(if (zerop n)
|
||||
"zero"
|
||||
(if (minusp n)
|
||||
"negative"
|
||||
"positive"
|
||||
"something else")))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "zero" (describe-number 0))
|
||||
(check-equal "negative" (describe-number -5))
|
||||
(check-equal "positive" (describe-number 3))
|
||||
@@ -0,0 +1,25 @@
|
||||
;;; CONDITIONALS 3: WHEN AND UNLESS
|
||||
;;;
|
||||
;;; WHEN runs its body when the test is true, and returns NIL
|
||||
;;; otherwise. UNLESS runs it when the test is false. Both take a
|
||||
;;; whole body of forms, but no else.
|
||||
|
||||
;; TODO: Squares of positives only: negative input gives NIL.
|
||||
(defun only-positive-squares (n)
|
||||
(when (minusp n)
|
||||
(* n n)))
|
||||
|
||||
;; TODO: :welcome when the door is open, :denied when it is not.
|
||||
(defun enter (door-open-p)
|
||||
(unless door-open-p
|
||||
:welcome))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal nil (only-positive-squares -3))
|
||||
(check-equal 9 (only-positive-squares 3))
|
||||
(check-equal :welcome (enter t))
|
||||
(check-equal :denied (enter nil))
|
||||
@@ -0,0 +1,26 @@
|
||||
;;; CONDITIONALS 4: AND AND OR
|
||||
;;;
|
||||
;;; AND and OR are conditionals that return values. AND stops at the
|
||||
;;; first NIL and returns it; otherwise it returns the last value.
|
||||
;;; OR stops at the first non-NIL and returns it. Nothing after the
|
||||
;;; stop is evaluated at all.
|
||||
|
||||
;; TODO: The first non-NIL argument, or NIL.
|
||||
(defun first-truthy (a b c)
|
||||
a)
|
||||
|
||||
;; TODO: A/B when B is not zero, else :undefined. Keep it lazy: the
|
||||
;;; division must never happen for a zero B.
|
||||
(defun safe-div (a b)
|
||||
(/ a b))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 2 (first-truthy nil 2 3))
|
||||
(check-equal 3 (first-truthy nil nil 3))
|
||||
(check-equal nil (first-truthy nil nil nil))
|
||||
(check-equal 5 (safe-div 10 2))
|
||||
(check-equal :undefined (safe-div 10 0))
|
||||
Reference in new issue
Block a user