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,15 @@
|
||||
;;; RECURSION 1: SUMMING A LIST (solution)
|
||||
|
||||
(defun sum (xs)
|
||||
(if (null xs)
|
||||
0
|
||||
(+ (car xs) (sum (cdr xs)))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 6 (sum '(1 2 3)))
|
||||
(check-equal 0 (sum '()))
|
||||
(check-equal 10 (sum '(1 2 3 4)))
|
||||
@@ -0,0 +1,15 @@
|
||||
;;; RECURSION 2: LENGTH (solution)
|
||||
|
||||
(defun my-length (xs)
|
||||
(if (null xs)
|
||||
0
|
||||
(+ 1 (my-length (cdr xs)))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 0 (my-length '()))
|
||||
(check-equal 3 (my-length '(a b c)))
|
||||
(check-equal 5 (my-length '(1 2 3 4 5)))
|
||||
@@ -0,0 +1,15 @@
|
||||
;;; RECURSION 3: MEMBERSHIP (solution)
|
||||
|
||||
(defun my-member (x xs)
|
||||
(cond ((null xs) nil)
|
||||
((equal x (car xs)) xs)
|
||||
(t (my-member x (cdr xs)))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(b c) (my-member 'b '(a b c)))
|
||||
(check-equal nil (my-member 'z '(a b c)))
|
||||
(check-equal '(1 2 3) (my-member 1 '(1 2 3)))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; RECURSION 4: WALKING A TREE (solution)
|
||||
|
||||
(defun count-leaves (tree)
|
||||
(cond ((null tree) 0)
|
||||
((atom tree) 1)
|
||||
(t (+ (count-leaves (car tree))
|
||||
(count-leaves (cdr tree))))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 0 (count-leaves '()))
|
||||
(check-equal 3 (count-leaves '(a b c)))
|
||||
(check-equal 4 (count-leaves '(1 (2 3) 4)))
|
||||
(check-equal 2 (count-leaves '((x) (y))))
|
||||
Reference in new issue
Block a user