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 @@
|
||||
# 09_strings
|
||||
|
||||
FORMAT directives, the string toolbox, joining and comparing.
|
||||
|
||||
FORMAT is a mini-language of directives: ~a prints, ~{...~} iterates.
|
||||
STRING-EQUAL compares ignoring case.
|
||||
@@ -0,0 +1,25 @@
|
||||
;;; STRINGS 1: FORMAT DIRECTIVES
|
||||
;;;
|
||||
;;; FORMAT is a little language: ~a prints readably, ~10a pads to a
|
||||
;;; width, ~% is a newline. The first argument NIL means "return a
|
||||
;;; string" instead of printing.
|
||||
;;; (format nil "Hello, ~a!" "Ada") => "Hello, Ada!"
|
||||
|
||||
;; TODO: "Ada has 42 cats." with the name and count in place.
|
||||
(defun cats (name count)
|
||||
(format nil "has cats."))
|
||||
|
||||
;; TODO: NAME left-justified in 10 columns, then an exclamation
|
||||
;;; mark.
|
||||
(defun banner (name)
|
||||
(format nil "Hello, ~a!" 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,28 @@
|
||||
;;; STRINGS 2: THE STRING TOOLBOX
|
||||
;;;
|
||||
;;; LENGTH counts characters. SUBSEQ slices: (subseq "hello" 1 3)
|
||||
;;; is "el". STRING-UPCASE shouts. SEARCH finds a substring and
|
||||
;;; returns its position - case-sensitively.
|
||||
|
||||
;; TODO: The first three characters of S.
|
||||
(defun first-three (s)
|
||||
(subseq s 1 3))
|
||||
|
||||
;; TODO: S uppercased with "!!" on the end.
|
||||
(defun shout (s)
|
||||
(string-downcase s))
|
||||
|
||||
;; TODO: The position of "lisp" inside S, or NIL.
|
||||
(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,23 @@
|
||||
;;; STRINGS 3: JOINING AND COMPARING
|
||||
;;;
|
||||
;;; CONCATENATE joins strings: (concatenate 'string "a" "b") =>
|
||||
;;; "ab". STRING= is exact; STRING-EQUAL ignores case.
|
||||
;;; Comparing strings with EQ is unreliable: strings are objects.
|
||||
|
||||
;; TODO: FIRST and LAST joined with one space between.
|
||||
(defun full-name (first last)
|
||||
(list first last))
|
||||
|
||||
;; TODO: T when FIRST and LAST are the same name, ignoring case.
|
||||
(defun same-name-p (first last)
|
||||
(eq 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,23 @@
|
||||
;;; STRINGS 4: FORMAT ITERATION
|
||||
;;;
|
||||
;;; ~{ ... ~} walks a list argument, and ~^ skips what follows at
|
||||
;;; the last element:
|
||||
;;; (format nil "~{~a~^, ~}" '(1 2 3)) => "1, 2, 3"
|
||||
|
||||
;; TODO: The numbers joined with ", ".
|
||||
(defun comma-list (xs)
|
||||
(format nil "~a" xs))
|
||||
|
||||
;; TODO: The names, one per line, with no trailing newline.
|
||||
(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")))
|
||||
Reference in new issue
Block a user