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,28 @@
|
||||
;;; LISPLINGS
|
||||
;;;
|
||||
;;; This first exercise has no errors. It is here to show you the flow:
|
||||
;;;
|
||||
;;; 1. Lisplings is watching this file. Every time you save it, the
|
||||
;;; exercise runs again in the terminal.
|
||||
;;; 2. The exercises that follow contain an error: the file does not
|
||||
;;; load, or a check at the bottom fails. Your job is to fix them.
|
||||
;;; 3. When everything passes, press n in this terminal to move on.
|
||||
;;; 4. Stuck? Press h for a hint.
|
||||
;;;
|
||||
;;; Try it now: add a line to the banner below, save the file, and
|
||||
;;; watch what happens in the terminal.
|
||||
|
||||
(format t "~%")
|
||||
(format t " /~%")
|
||||
(format t " /~%")
|
||||
(format t " /~%")
|
||||
(format t " /\\~%")
|
||||
(format t " / \\~%")
|
||||
(format t "/ \\~%")
|
||||
(format t "~%")
|
||||
(format t " Welcome to Lisplings!~%")
|
||||
(format t " Small exercises to get you used to reading and~%")
|
||||
(format t " writing Common Lisp code.~%~%")
|
||||
(format t "This exercise is exercises/00_intro/intro1.lisp.~%")
|
||||
(format t "Edit it in your editor of choice and save; the output~%")
|
||||
(format t "appears here automatically. Press n when you are ready.~%")
|
||||
@@ -0,0 +1,5 @@
|
||||
;;; INTRO 2: READING AN ERROR (solution)
|
||||
|
||||
(format t "Hello, ~a! Welcome to Common Lisp.~%" "world")
|
||||
|
||||
(format t "Save this file with your fix; lisplings runs it again.~%")
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; S-EXPRESSIONS 1: PREFIX NOTATION (solution)
|
||||
|
||||
(defun add-ten-and-thirty-two ()
|
||||
(+ 10 32))
|
||||
|
||||
(defun six-times-seven ()
|
||||
(* 6 7))
|
||||
|
||||
(defun fifty-from-one-hundred ()
|
||||
(- 100 50))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 42 (add-ten-and-thirty-two))
|
||||
(check-equal 42 (six-times-seven))
|
||||
(check-equal 50 (fifty-from-one-hundred))
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; S-EXPRESSIONS 2: NESTING AND EXACT MATH (solution)
|
||||
|
||||
(defun calc-a ()
|
||||
(* (+ 2 3) (- 4 1)))
|
||||
|
||||
(defun calc-b ()
|
||||
(/ 100 (+ 2 3)))
|
||||
|
||||
(defun one-half ()
|
||||
(/ 1 2))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 15 (calc-a))
|
||||
(check-equal 20 (calc-b))
|
||||
(check-equal 1/2 (one-half))
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; S-EXPRESSIONS 3: QUOTE (solution)
|
||||
|
||||
(defun the-form ()
|
||||
'(+ 1 2))
|
||||
|
||||
(defun the-symbol ()
|
||||
'hello)
|
||||
|
||||
(defun the-list ()
|
||||
'(a b c))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(+ 1 2) (the-form))
|
||||
(check-equal 'hello (the-symbol))
|
||||
(check-equal '(a b c) (the-list))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; FUNCTIONS 1: DEFUN (solution)
|
||||
|
||||
(defun twice (x)
|
||||
(* 2 x))
|
||||
|
||||
(defun add (a b)
|
||||
(+ a b))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 10 (twice 5))
|
||||
(check-equal -6 (twice -3))
|
||||
(check-equal 7 (add 3 4))
|
||||
(check-equal 0 (add 0 0))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; FUNCTIONS 2: ARITY AND ORDER (solution)
|
||||
|
||||
(defun average (a b)
|
||||
(/ (+ a b) 2))
|
||||
|
||||
(defun subtract (minuend subtrahend)
|
||||
(- minuend subtrahend))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 3 (average 2 4))
|
||||
(check-equal 0 (average 0 0))
|
||||
(check-equal 5 (subtract 9 4))
|
||||
(check-equal -5 (subtract 4 9))
|
||||
@@ -0,0 +1,18 @@
|
||||
;;; FUNCTIONS 3: OPTIONAL AND KEYWORD ARGUMENTS (solution)
|
||||
|
||||
(defun repeat-word (word &optional (n 2))
|
||||
(make-list n :initial-element word))
|
||||
|
||||
(defun make-box (width &key (height width) (depth width))
|
||||
(list :width width :height height :depth depth))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '("hi" "hi") (repeat-word "hi"))
|
||||
(check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4))
|
||||
(check-equal '(:width 2 :height 2 :depth 2) (make-box 2))
|
||||
(check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5))
|
||||
(check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7))
|
||||
@@ -0,0 +1,16 @@
|
||||
;;; FUNCTIONS 4: MULTIPLE VALUES (solution)
|
||||
|
||||
(defun divide (a b)
|
||||
(floor a b))
|
||||
|
||||
(defun split-seconds (seconds)
|
||||
(floor seconds 60))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(3 1) (multiple-value-list (divide 10 3)))
|
||||
(check-equal '(3 1) (multiple-value-list (divide 7 2)))
|
||||
(check-equal '(2 5) (multiple-value-list (split-seconds 125)))
|
||||
@@ -0,0 +1,14 @@
|
||||
;;; VARIABLES 1: GIVING A NAME TO A VALUE (solution)
|
||||
|
||||
(defparameter *room-capacity* 100)
|
||||
|
||||
(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,18 @@
|
||||
;;; VARIABLES 2: LET AND SHADOWING (solution)
|
||||
|
||||
(defun shadow-add ()
|
||||
(let ((x 15))
|
||||
(let ((x (+ x 5)))
|
||||
x)))
|
||||
|
||||
(defun wrap-me ()
|
||||
(let ((x 15))
|
||||
(+ 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,13 @@
|
||||
;;; VARIABLES 3: LET AND LET* (solution)
|
||||
|
||||
(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,16 @@
|
||||
;;; VARIABLES 4: SETF ON PLACES (solution)
|
||||
|
||||
(defparameter *account* (list :owner "Ada" :balance 100))
|
||||
|
||||
(defun deposit (amount)
|
||||
(setf (getf *account* :balance)
|
||||
(+ amount (getf *account* :balance)))
|
||||
*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))
|
||||
@@ -0,0 +1,15 @@
|
||||
;;; CONDITIONALS 1: IF (solution)
|
||||
|
||||
(defun classify (temperature)
|
||||
(if (> temperature 30)
|
||||
"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,15 @@
|
||||
;;; CONDITIONALS 2: COND (solution)
|
||||
|
||||
(defun describe-number (n)
|
||||
(cond ((zerop n) "zero")
|
||||
((minusp n) "negative")
|
||||
(t "positive")))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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,18 @@
|
||||
;;; CONDITIONALS 3: WHEN AND UNLESS (solution)
|
||||
|
||||
(defun only-positive-squares (n)
|
||||
(when (plusp n)
|
||||
(* n n)))
|
||||
|
||||
(defun enter (door-open-p)
|
||||
(if door-open-p :welcome :denied))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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,19 @@
|
||||
;;; CONDITIONALS 4: AND AND OR (solution)
|
||||
|
||||
(defun first-truthy (a b c)
|
||||
(or a b c))
|
||||
|
||||
(defun safe-div (a b)
|
||||
(or (unless (zerop b) (/ a b))
|
||||
:undefined))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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))
|
||||
@@ -0,0 +1,14 @@
|
||||
;;; PREDICATES 1: WHAT IS TRUE? (solution)
|
||||
|
||||
(defun truthy-p (thing)
|
||||
(not (null thing)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (truthy-p 0))
|
||||
(check (truthy-p ""))
|
||||
(check (not (truthy-p nil)))
|
||||
(check (not (truthy-p '())))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; PREDICATES 2: THE COMPARISON LADDER (solution)
|
||||
|
||||
(defun same-list-p (a b)
|
||||
(equal a b))
|
||||
|
||||
(defun same-number-p (a b)
|
||||
(= a b))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (same-list-p (list 1 2 3) (list 1 2 3)))
|
||||
(check (not (same-list-p (list 1 2 3) (list 1 3 2))))
|
||||
(check (same-number-p 1 1.0))
|
||||
(check (not (same-number-p 1 2)))
|
||||
@@ -0,0 +1,18 @@
|
||||
;;; PREDICATES 3: ASKING ABOUT TYPES (solution)
|
||||
|
||||
(defun describe-value (v)
|
||||
(cond ((null v) :nothing)
|
||||
((numberp v) :number)
|
||||
((stringp v) :string)
|
||||
((listp v) :list)
|
||||
(t :other)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal :number (describe-value 3))
|
||||
(check-equal :string (describe-value "hi"))
|
||||
(check-equal :list (describe-value '(a b)))
|
||||
(check-equal :nothing (describe-value nil))
|
||||
@@ -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)))))
|
||||
@@ -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))))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; FUNCTIONAL 1: LAMBDA AND FUNCALL (solution)
|
||||
|
||||
(defun apply-to-ten (f)
|
||||
(funcall f 10))
|
||||
|
||||
(defun doubler ()
|
||||
(lambda (x) (* 2 x)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 100 (apply-to-ten (lambda (x) (* x x))))
|
||||
(check-equal 10 (apply-to-ten (lambda (x) (- x 0))))
|
||||
(check-equal 8 (funcall (doubler) 4))
|
||||
(check-equal -2 (funcall (doubler) -1))
|
||||
@@ -0,0 +1,16 @@
|
||||
;;; FUNCTIONAL 2: MAPCAR (solution)
|
||||
|
||||
(defun double-all (xs)
|
||||
(mapcar (lambda (x) (* 2 x)) xs))
|
||||
|
||||
(defun shout-names (names)
|
||||
(mapcar #'string-upcase names))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(2 4 6) (double-all '(1 2 3)))
|
||||
(check-equal '(0) (double-all '(0)))
|
||||
(check-equal '("ADA" "GRACE") (shout-names '("ada" "grace")))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; FUNCTIONAL 3: FUNCALL AND APPLY (solution)
|
||||
|
||||
(defun sum-of (xs)
|
||||
(apply #'+ xs))
|
||||
|
||||
(defun call-with-more (f a xs)
|
||||
(apply f a xs))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 6 (sum-of '(1 2 3)))
|
||||
(check-equal 0 (sum-of '()))
|
||||
(check-equal 10 (call-with-more #'+ 1 '(2 3 4)))
|
||||
(check-equal 5 (call-with-more #'max 1 '(5 2)))
|
||||
@@ -0,0 +1,18 @@
|
||||
;;; FUNCTIONAL 4: REDUCE (solution)
|
||||
|
||||
(defun biggest (xs)
|
||||
(reduce #'max xs))
|
||||
|
||||
(defun product (xs)
|
||||
(reduce #'* xs :initial-value 1))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 4 (biggest '(3 1 4)))
|
||||
(check-equal 2 (biggest '(2)))
|
||||
(check-equal 6 (product '(1 2 3)))
|
||||
(check-equal 7 (product '(7)))
|
||||
(check-equal 1 (product '()))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; STRINGS 1: FORMAT DIRECTIVES (solution)
|
||||
|
||||
(defun cats (name count)
|
||||
(format nil "~a has ~a cats." name count))
|
||||
|
||||
(defun banner (name)
|
||||
(format nil "~10a!" name))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "Ada has 42 cats." (cats "Ada" 42))
|
||||
(check-equal "Bob has 7 cats." (cats "Bob" 7))
|
||||
(check-equal "Ada !" (banner "Ada"))
|
||||
(check-equal "Grace !" (banner "Grace"))
|
||||
@@ -0,0 +1,21 @@
|
||||
;;; STRINGS 2: THE STRING TOOLBOX (solution)
|
||||
|
||||
(defun first-three (s)
|
||||
(subseq s 0 3))
|
||||
|
||||
(defun shout (s)
|
||||
(concatenate 'string (string-upcase s) "!!"))
|
||||
|
||||
(defun find-lisp (s)
|
||||
(search "lisp" s))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "hel" (first-three "hello"))
|
||||
(check-equal "ADA!!" (shout "ada"))
|
||||
(check-equal "GRACE!!" (shout "grace"))
|
||||
(check-equal 2 (find-lisp "i lisp things"))
|
||||
(check-equal nil (find-lisp "not here"))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; STRINGS 3: JOINING AND COMPARING (solution)
|
||||
|
||||
(defun full-name (first last)
|
||||
(concatenate 'string first " " last))
|
||||
|
||||
(defun same-name-p (first last)
|
||||
(string-equal first last))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "Ada Lovelace" (full-name "Ada" "Lovelace"))
|
||||
(check-equal "Alan Turing" (full-name "Alan" "Turing"))
|
||||
(check (same-name-p "Ada" "ADA"))
|
||||
(check (not (same-name-p "Ada" "Grace")))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; STRINGS 4: FORMAT ITERATION (solution)
|
||||
|
||||
(defun comma-list (xs)
|
||||
(format nil "~{~a~^, ~}" xs))
|
||||
|
||||
(defun lines (names)
|
||||
(format nil "~{~a~^~%~}" names))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "1, 2, 3" (comma-list '(1 2 3)))
|
||||
(check-equal "" (comma-list '()))
|
||||
(check-equal "ada
|
||||
grace" (lines '("ada" "grace")))
|
||||
@@ -0,0 +1,23 @@
|
||||
;;; LOOPS 1: DOLIST AND DOTIMES (solution)
|
||||
|
||||
(defun sum-of (xs)
|
||||
(let ((total 0))
|
||||
(dolist (x xs)
|
||||
(incf total x))
|
||||
total))
|
||||
|
||||
(defun countdown (n)
|
||||
(let ((acc '()))
|
||||
(dotimes (i n)
|
||||
(push i acc))
|
||||
acc))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 6 (sum-of '(1 2 3)))
|
||||
(check-equal 0 (sum-of '()))
|
||||
(check-equal '(2 1 0) (countdown 3))
|
||||
(check-equal '() (countdown 0))
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; LOOPS 2: THE LOOP MACRO (solution)
|
||||
|
||||
(defun squares (xs)
|
||||
(loop for x in xs
|
||||
collect (* x x)))
|
||||
|
||||
(defun triangle (n)
|
||||
(loop for i from 1 to n
|
||||
sum i))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(1 4 9) (squares '(1 2 3)))
|
||||
(check-equal 6 (triangle 3))
|
||||
(check-equal 55 (triangle 10))
|
||||
(check-equal 0 (triangle 0))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; LOOPS 3: LOOP WITH TESTS (solution)
|
||||
|
||||
(defun evens (xs)
|
||||
(loop for x in xs
|
||||
when (evenp x)
|
||||
collect x))
|
||||
|
||||
(defun first-big (xs)
|
||||
(loop for x in xs
|
||||
thereis (if (> x 100) x)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(2 4 6) (evens '(1 2 3 4 5 6)))
|
||||
(check-equal '() (evens '(1 3)))
|
||||
(check-equal 150 (first-big '(5 150 250 3)))
|
||||
(check-equal nil (first-big '(1 2 3)))
|
||||
@@ -0,0 +1,15 @@
|
||||
;;; NUMBERS 1: THE TOWER (solution)
|
||||
|
||||
(defun two-to-the-hundred ()
|
||||
(expt 2 100))
|
||||
|
||||
(defun half-of-seven ()
|
||||
(/ 7 2))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 1267650600228229401496703205376 (two-to-the-hundred))
|
||||
(check-equal 7/2 (half-of-seven))
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; NUMBERS 2: ROUNDING AND REMAINDERS (solution)
|
||||
|
||||
(defun floor-to-five (x)
|
||||
(* 5 (floor x 5)))
|
||||
|
||||
(defun mod-five (x)
|
||||
(mod x 5))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 10 (floor-to-five 12))
|
||||
(check-equal 10 (floor-to-five 14))
|
||||
(check-equal 15 (floor-to-five 15))
|
||||
(check-equal 4 (mod-five 9))
|
||||
(check-equal 0 (mod-five -5))
|
||||
(check-equal 1 (mod-five -9))
|
||||
@@ -0,0 +1,27 @@
|
||||
;;; NUMBERS 3: BOUNDS AND STEPS (solution)
|
||||
|
||||
(defun clamp (x low high)
|
||||
(min high (max low x)))
|
||||
|
||||
(defun distance-1d (a b)
|
||||
(abs (- a b)))
|
||||
|
||||
(defun walk ()
|
||||
(let ((n 10))
|
||||
(incf n)
|
||||
(decf n 2)
|
||||
(incf n 5)
|
||||
n))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 5 (clamp 12 0 5))
|
||||
(check-equal 0 (clamp -3 0 5))
|
||||
(check-equal 3 (clamp 3 0 5))
|
||||
(check-equal 3 (distance-1d 5 2))
|
||||
(check-equal 3 (distance-1d 2 5))
|
||||
(check-equal 0 (distance-1d 4 4))
|
||||
(check-equal 14 (walk))
|
||||
@@ -0,0 +1,21 @@
|
||||
;;; STRUCTS 1: DEFSTRUCT (solution)
|
||||
|
||||
(defstruct person
|
||||
name
|
||||
age)
|
||||
|
||||
(defun make-ada ()
|
||||
(make-person :name "Ada" :age 36))
|
||||
|
||||
(defun ada-age (ada)
|
||||
(person-age ada))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((ada (make-ada)))
|
||||
(and (equal "Ada" (person-name ada))
|
||||
(eql 36 (person-age ada)))))
|
||||
(check (eql 36 (ada-age (make-ada))))
|
||||
@@ -0,0 +1,23 @@
|
||||
;;; STRUCTS 2: DEFAULTS AND PREDICATES (solution)
|
||||
|
||||
(defstruct spaceship
|
||||
(name "unnamed")
|
||||
(crew-size 1))
|
||||
|
||||
(defun seven-crew ()
|
||||
(make-spaceship :crew-size 7))
|
||||
|
||||
(defun ship-p (x)
|
||||
(spaceship-p x))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((s (seven-crew)))
|
||||
(and (equal "unnamed" (spaceship-name s))
|
||||
(eql 7 (spaceship-crew-size s)))))
|
||||
(check (and (ship-p (make-spaceship))
|
||||
(not (ship-p "a plain string"))
|
||||
(not (ship-p 42))))
|
||||
@@ -0,0 +1,24 @@
|
||||
;;; STRUCTS 3: MUTATION (solution)
|
||||
|
||||
(defstruct counter
|
||||
(value 0))
|
||||
|
||||
(defun bump (c)
|
||||
(incf (counter-value c))
|
||||
c)
|
||||
|
||||
(defun reset-counter (c)
|
||||
(setf (counter-value c) 0)
|
||||
c)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((c (make-counter)))
|
||||
(bump c)
|
||||
(eql 1 (counter-value c))))
|
||||
(check (let ((c (make-counter :value 9)))
|
||||
(reset-counter c)
|
||||
(eql 0 (counter-value c))))
|
||||
@@ -0,0 +1,24 @@
|
||||
;;; STRUCTS 4: INHERITANCE (solution)
|
||||
|
||||
(defstruct person
|
||||
name
|
||||
age)
|
||||
|
||||
(defstruct (student (:include person))
|
||||
major)
|
||||
|
||||
(defun make-zed ()
|
||||
(make-student :name "Zed" :age 21 :major "physics"))
|
||||
|
||||
(defun zed-major (zed)
|
||||
(student-major zed))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((zed (make-zed)))
|
||||
(and (equal "Zed" (student-name zed))
|
||||
(eql 21 (student-age zed))
|
||||
(equal "physics" (zed-major zed)))))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; HASH TABLES 1: BASICS (solution)
|
||||
|
||||
(defun people ()
|
||||
(let ((table (make-hash-table)))
|
||||
(setf (gethash 'ada table) 36
|
||||
(gethash 'grace table) 45)
|
||||
table))
|
||||
|
||||
(defun age-of (name table)
|
||||
(gethash name table))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((table (people)))
|
||||
(and (eql 36 (age-of 'ada table))
|
||||
(eql 45 (age-of 'grace table))
|
||||
(eq nil (age-of 'alan table)))))
|
||||
@@ -0,0 +1,25 @@
|
||||
;;; HASH TABLES 2: IS THE KEY THERE? (solution)
|
||||
|
||||
(defun has-key-p (key table)
|
||||
(nth-value 1 (gethash key table)))
|
||||
|
||||
(defun lookup (key table default)
|
||||
(multiple-value-bind (value present)
|
||||
(gethash key table)
|
||||
(if present value default)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((table (make-hash-table)))
|
||||
(setf (gethash 'a table) 1
|
||||
(gethash 'b table) nil)
|
||||
(and (has-key-p 'a table)
|
||||
(has-key-p 'b table)
|
||||
(not (has-key-p 'c table)))))
|
||||
(check (let ((table (make-hash-table)))
|
||||
(setf (gethash 'a table) 1)
|
||||
(and (eql 1 (lookup 'a table 0))
|
||||
(eql 99 (lookup 'c table 99)))))
|
||||
@@ -0,0 +1,18 @@
|
||||
;;; HASH TABLES 3: COUNTING (solution)
|
||||
|
||||
(defun tally (xs)
|
||||
(let ((table (make-hash-table)))
|
||||
(dolist (x xs)
|
||||
(incf (gethash x table 0)))
|
||||
table))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((table (tally '(a b a c a b))))
|
||||
(and (eql 3 (gethash 'a table))
|
||||
(eql 2 (gethash 'b table))
|
||||
(eql 1 (gethash 'c table))
|
||||
(eql 3 (hash-table-count table)))))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; PACKAGES 1: DEFPACKAGE (solution)
|
||||
|
||||
(defpackage #:shapes
|
||||
(:use #:common-lisp)
|
||||
(:export #:area))
|
||||
|
||||
(in-package #:shapes)
|
||||
|
||||
(defun area (s)
|
||||
(* s s))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged. (Checks run back in the user package.)
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(in-package #:common-lisp-user)
|
||||
|
||||
(check-equal 25 (shapes:area 5))
|
||||
(check-equal 9 (shapes:area 3))
|
||||
@@ -0,0 +1,19 @@
|
||||
;;; PACKAGES 2: EXPORTS (solution)
|
||||
|
||||
(defpackage #:kitchen
|
||||
(:use #:common-lisp)
|
||||
(:export #:make-soup))
|
||||
|
||||
(in-package #:kitchen)
|
||||
|
||||
(defun make-soup ()
|
||||
"a pot of soup")
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged. (Checks run back in the user package.)
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(in-package #:common-lisp-user)
|
||||
|
||||
(check-equal "a pot of soup" (kitchen:make-soup))
|
||||
@@ -0,0 +1,29 @@
|
||||
;;; PACKAGES 3: USING ANOTHER PACKAGE (solution)
|
||||
|
||||
(defpackage #:shapes
|
||||
(:use #:common-lisp)
|
||||
(:export #:area))
|
||||
|
||||
(in-package #:shapes)
|
||||
|
||||
(defun area (s)
|
||||
(* s s))
|
||||
|
||||
(defpackage #:studio
|
||||
(:use #:common-lisp #:shapes)
|
||||
(:export #:total))
|
||||
|
||||
(in-package #:studio)
|
||||
|
||||
(defun total (a b)
|
||||
(+ (area a) (area b)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged. (Checks run back in the user package.)
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(in-package #:common-lisp-user)
|
||||
|
||||
(check-equal 13 (studio:total 2 3))
|
||||
(check-equal 5 (studio:total 1 2))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; CLOS 1: CLASSES AND INSTANCES (solution)
|
||||
|
||||
(defclass dog ()
|
||||
((name :initarg :name :accessor dog-name)
|
||||
(tricks :initarg :tricks :accessor dog-tricks :initform '())))
|
||||
|
||||
(defun make-rex ()
|
||||
(make-instance 'dog :name "Rex" :tricks '("sit" "roll-over")))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((rex (make-rex)))
|
||||
(and (string= "Rex" (dog-name rex))
|
||||
(equal '("sit" "roll-over") (dog-tricks rex)))))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; CLOS 2: GENERIC FUNCTIONS (solution)
|
||||
|
||||
(defclass animal () ())
|
||||
(defclass dog (animal) ())
|
||||
(defclass cat (animal) ())
|
||||
|
||||
(defgeneric speak (a))
|
||||
|
||||
(defmethod speak ((a dog)) "woof")
|
||||
(defmethod speak ((a cat)) "meow")
|
||||
(defmethod speak ((a animal)) "...")
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "woof" (speak (make-instance 'dog)))
|
||||
(check-equal "meow" (speak (make-instance 'cat)))
|
||||
(check-equal "..." (speak (make-instance 'animal)))
|
||||
@@ -0,0 +1,27 @@
|
||||
;;; CLOS 3: COMBINING METHODS (solution)
|
||||
|
||||
(defclass account ()
|
||||
((log :initform '() :accessor account-log)
|
||||
(balance :initform 0 :accessor account-balance)))
|
||||
|
||||
(defgeneric deposit (account amount))
|
||||
|
||||
(defmethod deposit ((a account) amount)
|
||||
(incf (account-balance a) amount))
|
||||
|
||||
(defmethod deposit :before ((a account) amount)
|
||||
(push (list 'depositing amount) (account-log a)))
|
||||
|
||||
(defmethod deposit :after ((a account) amount)
|
||||
(push (list 'deposited amount) (account-log a)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((a (make-instance 'account)))
|
||||
(deposit a 50)
|
||||
(and (eql 50 (account-balance a))
|
||||
(equal '((deposited 50) (depositing 50))
|
||||
(account-log a)))))
|
||||
@@ -0,0 +1,17 @@
|
||||
;;; CLOS 4: DISPATCH ON A VALUE (solution)
|
||||
|
||||
(defgeneric serve (customer))
|
||||
|
||||
(defmethod serve (customer)
|
||||
"one espresso")
|
||||
|
||||
(defmethod serve ((customer (eql 'vip)))
|
||||
"a free muffin too")
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "a free muffin too" (serve 'vip))
|
||||
(check-equal "one espresso" (serve 'ada))
|
||||
@@ -0,0 +1,14 @@
|
||||
;;; CONDITIONS 1: ERROR AND HANDLER-CASE (solution)
|
||||
|
||||
(defun safe-parse (string)
|
||||
(handler-case (parse-integer string)
|
||||
(parse-error () :not-a-number)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 42 (safe-parse "42"))
|
||||
(check-equal -7 (safe-parse "-7"))
|
||||
(check-equal :not-a-number (safe-parse "hello"))
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; CONDITIONS 2: UNWIND-PROTECT (solution)
|
||||
|
||||
(defparameter *lid* :closed)
|
||||
|
||||
(defun with-lid (f)
|
||||
(unwind-protect
|
||||
(progn (setf *lid* :open)
|
||||
(funcall f))
|
||||
(setf *lid* :closed)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (progn (with-lid (lambda () 1))
|
||||
(eq :closed *lid*)))
|
||||
(check (handler-case
|
||||
(progn (with-lid (lambda () (error "oops")))
|
||||
nil)
|
||||
(error () t)))
|
||||
(check (eq :closed *lid*))
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; CONDITIONS 3: YOUR OWN CONDITION TYPES (solution)
|
||||
|
||||
(define-condition empty-plate (error)
|
||||
((what :initarg :what :reader empty-plate-what))
|
||||
(:documentation "Signaled when a plate turns out to be empty."))
|
||||
|
||||
(defun eat (xs)
|
||||
(if (null xs)
|
||||
(error 'empty-plate :what "soup")
|
||||
"yum"))
|
||||
|
||||
(defun catch-empty (xs)
|
||||
(handler-case (eat xs)
|
||||
(empty-plate (e) (empty-plate-what e))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "yum" (catch-empty '(a b)))
|
||||
(check-equal "soup" (catch-empty '()))
|
||||
@@ -0,0 +1,23 @@
|
||||
;;; CONDITIONS 4: RESTARTS (solution)
|
||||
|
||||
(defun safe-car (xs)
|
||||
(restart-case
|
||||
(if (consp xs)
|
||||
(car xs)
|
||||
(error "safe-car: ~s is not a cons" xs))
|
||||
(use-nil () nil)))
|
||||
|
||||
(defun car-with-default (xs)
|
||||
(handler-bind ((error (lambda (e)
|
||||
(declare (ignore e))
|
||||
(invoke-restart 'use-nil))))
|
||||
(safe-car xs)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 1 (car-with-default '(1 2 3)))
|
||||
(check-equal nil (car-with-default "not a list"))
|
||||
(check-equal nil (car-with-default nil))
|
||||
@@ -0,0 +1,13 @@
|
||||
;;; MACROS 1: DEFMACRO (solution)
|
||||
|
||||
(defmacro my-when (test &body body)
|
||||
`(if ,test (progn ,@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,16 @@
|
||||
;;; MACROS 2: BACKQUOTE, COMMA, SPLICE (solution)
|
||||
|
||||
(defun wrap-value (x)
|
||||
`(boxed ,x))
|
||||
|
||||
(defun make-add-form (numbers)
|
||||
`(add ,@numbers))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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,16 @@
|
||||
;;; MACROS 3: GENSYM AND HYGIENE (solution)
|
||||
|
||||
(defmacro n-plus-five (n)
|
||||
(let ((temp (gensym)))
|
||||
`(let ((,temp 5))
|
||||
(+ ,n ,temp))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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,18 @@
|
||||
;;; MACROS 4: A MACRO OF YOUR OWN (solution)
|
||||
|
||||
(defmacro while (test &body body)
|
||||
`(loop while ,test do ,@body))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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))
|
||||
@@ -0,0 +1,15 @@
|
||||
;;; DYNAMIC 1: CLOSURES (solution)
|
||||
|
||||
(defun make-counter ()
|
||||
(let ((count 0))
|
||||
(lambda () (incf count))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((c1 (make-counter))
|
||||
(c2 (make-counter)))
|
||||
(and (equal '(1 2 3) (list (funcall c1) (funcall c1) (funcall c1)))
|
||||
(eql 1 (funcall c2)))))
|
||||
@@ -0,0 +1,20 @@
|
||||
;;; DYNAMIC 2: SPECIAL VARIABLES (solution)
|
||||
|
||||
(defparameter *depth* 0)
|
||||
|
||||
(defun current-depth ()
|
||||
*depth*)
|
||||
|
||||
(defun with-depth (n f)
|
||||
(let ((*depth* n))
|
||||
(funcall f)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (eql 7 (with-depth 7 (lambda () (current-depth)))))
|
||||
(check (eql 0 (current-depth)))
|
||||
(check (eql 3 (let ((*depth* 3)) (current-depth))))
|
||||
(check (eql 0 (current-depth)))
|
||||
@@ -0,0 +1,29 @@
|
||||
;;; DYNAMIC 3: A MEMOIZING CLOSURE (solution)
|
||||
|
||||
(defparameter *fib-calls* 0)
|
||||
|
||||
(defun slow-fib (n)
|
||||
(incf *fib-calls*)
|
||||
(if (< n 2)
|
||||
n
|
||||
(+ (slow-fib (- n 1)) (slow-fib (- n 2)))))
|
||||
|
||||
(defun make-memo-fib ()
|
||||
(let ((table (make-hash-table)))
|
||||
(lambda (n)
|
||||
(multiple-value-bind (value present) (gethash n table)
|
||||
(if present
|
||||
value
|
||||
(setf (gethash n table) (slow-fib n)))))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((fib (make-memo-fib)))
|
||||
(setf *fib-calls* 0)
|
||||
(and (eql 55 (funcall fib 10))
|
||||
(let ((after-first *fib-calls*))
|
||||
(and (eql 55 (funcall fib 10))
|
||||
(eql after-first *fib-calls*))))))
|
||||
@@ -0,0 +1,32 @@
|
||||
;;; QUIZ 1: THE CONTACT BOOK (solution)
|
||||
|
||||
(defparameter *book*
|
||||
(list (list :name "Ada" :email "ada@example.com")
|
||||
(list :name "Grace" :email "grace@example.com")))
|
||||
|
||||
(defun add-contact (book name email)
|
||||
(append book (list (list :name name :email email))))
|
||||
|
||||
(defun find-contact (book name)
|
||||
(dolist (contact book)
|
||||
(when (string-equal name (getf contact :name))
|
||||
(return contact))))
|
||||
|
||||
(defun contact-card (contact)
|
||||
(format nil "~a <~a>" (getf contact :name) (getf contact :email)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (equal (list (list :name "Ada" :email "ada@example.com")
|
||||
(list :name "Grace" :email "grace@example.com")
|
||||
(list :name "Zed" :email "zed@example.com"))
|
||||
(add-contact *book* "Zed" "zed@example.com")))
|
||||
(check (equal '(:name "Zed" :email "zed@example.com")
|
||||
(find-contact (add-contact *book* "Zed" "zed@example.com")
|
||||
"zed")))
|
||||
(check-equal nil (find-contact *book* "nope"))
|
||||
(check-equal "Ada <ada@example.com>"
|
||||
(contact-card (find-contact *book* "ada")))
|
||||
@@ -0,0 +1,22 @@
|
||||
;;; QUIZ 2: A TINY LISP, IN LISP (solution)
|
||||
|
||||
(defun eval-expr (expr)
|
||||
(cond ((numberp expr) expr)
|
||||
((and (consp expr) (eq 'quote (car expr)))
|
||||
(second expr))
|
||||
((and (consp expr) (member (car expr) '(+ - *)))
|
||||
(apply (car expr) (mapcar #'eval-expr (cdr expr))))
|
||||
(t (error "cannot evaluate ~s" expr))))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 7 (eval-expr 7))
|
||||
(check-equal 3 (eval-expr '(+ 1 2)))
|
||||
(check-equal 12 (eval-expr '(* (+ 1 2) 4)))
|
||||
(check-equal '(a b c) (eval-expr '(quote (a b c))))
|
||||
(check-equal -1 (eval-expr '(- (* 2 3) (+ 1 (+ 2 4)))))
|
||||
(check-error (eval-expr 'foo))
|
||||
(check-error (eval-expr '(lambda (x) x)))
|
||||
Reference in new issue
Block a user