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,20 @@
|
||||
;;; LISTS 1: CONS, CAR, CDR (solution)
|
||||
|
||||
(defun first-of (xs)
|
||||
(car xs))
|
||||
|
||||
(defun rest-of (xs)
|
||||
(cdr xs))
|
||||
|
||||
(defun prepend (x xs)
|
||||
(cons x xs))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 1 (first-of '(1 2 3)))
|
||||
(check-equal '(2 3) (rest-of '(1 2 3)))
|
||||
(check-equal '(0 1 2) (prepend 0 '(1 2)))
|
||||
(check-equal '(a) (prepend 'a nil))
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; LISTS 2: LIST, QUOTE, AND APPEND (solution)
|
||||
|
||||
(defun abc ()
|
||||
'(a b c))
|
||||
|
||||
(defun one-two-three ()
|
||||
(list 1 2 3))
|
||||
|
||||
(defun joined ()
|
||||
(append '(1 2) '(3 4)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(a b c) (abc))
|
||||
(check-equal '(1 2 3) (one-two-three))
|
||||
(check-equal '(1 2 3 4) (joined))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; LISTS 3: ELEMENTS BY POSITION (solution)
|
||||
|
||||
(defun my-first (xs)
|
||||
(nth 0 xs))
|
||||
|
||||
(defun my-last (xs)
|
||||
(car (last xs)))
|
||||
|
||||
(defun drop-last (xs)
|
||||
(butlast xs))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 'a (my-first '(a b c)))
|
||||
(check-equal 'c (my-last '(a b c)))
|
||||
(check-equal '(a b) (drop-last '(a b c)))
|
||||
(check-equal nil (drop-last '(a)))
|
||||
@@ -0,0 +1,21 @@
|
||||
;;; LISTS 4: PUSH AND POP (solution)
|
||||
|
||||
(defun push-two (xs)
|
||||
(let ((ys xs))
|
||||
(push 0 ys)
|
||||
(push 1 ys)
|
||||
ys))
|
||||
|
||||
(defun skip-two (xs)
|
||||
(let ((ys xs))
|
||||
(pop ys)
|
||||
(pop ys)
|
||||
ys))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(1 0 a b) (push-two '(a b)))
|
||||
(check-equal '(c d) (skip-two '(a b c d)))
|
||||
@@ -0,0 +1,23 @@
|
||||
;;; LISTS 5: ASSOCIATION LISTS (solution)
|
||||
|
||||
(defparameter *menu*
|
||||
'((coffee . 3) (tea . 2) (cocoa . 4)))
|
||||
|
||||
(defun price (item)
|
||||
(cdr (assoc item *menu*)))
|
||||
|
||||
(defun add-item (item cost)
|
||||
(push (cons item cost) *menu*)
|
||||
*menu*)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 3 (price 'coffee))
|
||||
(check-equal 2 (price 'tea))
|
||||
(check-equal nil (price 'chai))
|
||||
(check (let ((menu (add-item 'chai 5)))
|
||||
(and (eql 5 (price 'chai))
|
||||
(eql 3 (price 'coffee)))))
|
||||
Reference in new issue
Block a user