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,7 @@
|
||||
# 02_functions
|
||||
|
||||
DEFUN, arity, optional and keyword arguments, and multiple values.
|
||||
|
||||
The return value is simply the last form of the body - no `return`
|
||||
keyword needed. Extra return values ride along with VALUES, and
|
||||
functions like FLOOR hand them out for free.
|
||||
@@ -0,0 +1,24 @@
|
||||
;;; FUNCTIONS 1: DEFUN
|
||||
;;;
|
||||
;;; (defun name (parameters) body...)
|
||||
;;;
|
||||
;;; The body may hold many forms; they run in order, and the LAST
|
||||
;;; one is the return value.
|
||||
|
||||
;; TODO: Return twice the argument.
|
||||
(defun twice (x)
|
||||
x)
|
||||
|
||||
;; TODO: Add the two arguments.
|
||||
(defun add (a b)
|
||||
a)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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,24 @@
|
||||
;;; FUNCTIONS 2: ARITY AND ORDER
|
||||
;;;
|
||||
;;; Lisp checks how many arguments you pass, and its error messages
|
||||
;;; say exactly what went wrong. Read them carefully.
|
||||
|
||||
;; TODO: The calls below pass TWO numbers. Give average the right
|
||||
;;; parameter list, and make it return their average.
|
||||
(defun average (a b c)
|
||||
(/ (+ a b c) 3))
|
||||
|
||||
;; TODO: (subtract 9 4) should be 5. Mind which way round you wrote
|
||||
;;; the subtraction.
|
||||
(defun subtract (minuend subtrahend)
|
||||
(- subtrahend minuend))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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,26 @@
|
||||
;;; FUNCTIONS 3: OPTIONAL AND KEYWORD ARGUMENTS
|
||||
;;;
|
||||
;;; (defun f (word &optional (n 2)) ...) N is optional, defaults to 2.
|
||||
;;; (defun g (x &key (y x)) ...) Call as (g 3 :y 5); a
|
||||
;;; default may use an
|
||||
;;; earlier parameter.
|
||||
|
||||
;; TODO: Repeat WORD N times. N is optional and defaults to 2.
|
||||
(defun repeat-word (word n)
|
||||
(make-list n :initial-element word))
|
||||
|
||||
;; TODO: Describe a box: WIDTH is required; :HEIGHT and :DEPTH are
|
||||
;;; keywords, both defaulting to the width.
|
||||
(defun make-box (width &key height depth)
|
||||
(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,24 @@
|
||||
;;; FUNCTIONS 4: MULTIPLE VALUES
|
||||
;;;
|
||||
;;; A function may return several values at once:
|
||||
;;; (values 1 2) => 1 and 2
|
||||
;;; Everything except the first value rides along, waiting for
|
||||
;;; MULTIPLE-VALUE-BIND or NTH-VALUE to pick it up. FLOOR is a
|
||||
;;; classic: (floor 10 3) returns 3 AND 1.
|
||||
|
||||
;; TODO: Return TWO values: the whole part of A/B and the rest.
|
||||
(defun divide (a b)
|
||||
(/ a b))
|
||||
|
||||
;; TODO: Return TWO values: whole minutes, and leftover seconds.
|
||||
(defun split-seconds (seconds)
|
||||
seconds)
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; 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)))
|
||||
Reference in new issue
Block a user