Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table
This commit is contained in:
1 parent
77843402fa
commit
4675bd1106
76 files changed
+2505
-14
No files matched your search
@@ -0,0 +1,26 @@
|
||||
;;; STRINGS 5: NUMBERS IN FORMAT
|
||||
;;;
|
||||
;;; ~a prints any value plainly. ~d and ~f print numbers with more
|
||||
;;; care, and both take modifiers: a COLON on ~d groups the digits,
|
||||
;;; (format nil "~:d" 1234567) => "1,234,567"
|
||||
;;; and a number after a comma on ~f fixes the decimals,
|
||||
;;; (format nil "~,2f" pi) => "3.14"
|
||||
|
||||
;; TODO: N widgets, grouped for humans: "1,234,567 widgets".
|
||||
(defun price-tag (n)
|
||||
(format nil "~d widgets" n))
|
||||
|
||||
;; TODO: LABEL is about N, with exactly two decimals:
|
||||
;; "pi is about 3.14".
|
||||
(defun fixed (label n)
|
||||
(format nil "~a is about ~a" label n))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "1,234,567 widgets" (price-tag 1234567))
|
||||
(check-equal "7 widgets" (price-tag 7))
|
||||
(check-equal "pi is about 3.14" (fixed "pi" pi))
|
||||
(check-equal "e is about 2.72" (fixed "e" 2.718281828))
|
||||
@@ -0,0 +1,32 @@
|
||||
;;; STRINGS 6: FORMAT PICKS AND CASE
|
||||
;;;
|
||||
;;; FORMAT has its own conditional, for choosing between texts:
|
||||
;;;
|
||||
;;; (format nil "~[tick~;tock~]" 1) => "tock"
|
||||
;;;
|
||||
;;; The argument is a number: 0 picks the first branch, 1 the
|
||||
;;; second. And ~( ... ~) folds everything between them to lower
|
||||
;;; case - STRING-DOWNCASE in FORMAT's clothing:
|
||||
;;;
|
||||
;;; (format nil "~(~a~)" "GRACE") => "grace"
|
||||
|
||||
;; TODO: "tick" on an even N, "tock" on an odd one - let FORMAT
|
||||
;; choose.
|
||||
(defun tick-tock (n)
|
||||
(format nil "tick"))
|
||||
|
||||
;; TODO: The title S, downcased.
|
||||
(defun quiet-title (s)
|
||||
(format nil "~a" s))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "tick" (tick-tock 0))
|
||||
(check-equal "tock" (tick-tock 1))
|
||||
(check-equal "tick" (tick-tock 2))
|
||||
(check-equal "tock" (tick-tock 5))
|
||||
(check-equal "grace" (quiet-title "GRACE"))
|
||||
(check-equal "ada lovelace" (quiet-title "ADA LOVELACE"))
|
||||
@@ -0,0 +1,38 @@
|
||||
;;; STRINGS 7: NEWLINES AND COLUMNS
|
||||
;;;
|
||||
;;; ~% ALWAYS breaks the line. ~& breaks one only when you are not
|
||||
;;; at the start of a line already - fresh line, not newline - so a
|
||||
;;; brand-new string gets nothing:
|
||||
;;;
|
||||
;;; (format nil "~&~a" "hi") => "hi"
|
||||
;;;
|
||||
;;; ~v takes the field width from the arguments, and @ pads on the
|
||||
;;; left - right-justified:
|
||||
;;;
|
||||
;;; (format nil "~v@a" 6 "ada") => " ada"
|
||||
|
||||
;; TODO: S on a fresh line: nothing before it when the line is
|
||||
;; already new.
|
||||
(defun add-line (s)
|
||||
(format nil "~%~a" s))
|
||||
|
||||
;; TODO: NAME right-justified in a field WIDTH wide.
|
||||
(defun right-column (name width)
|
||||
(format nil "~va" width name))
|
||||
|
||||
;; TODO: A receipt row: ITEM in an 8-wide column, PRICE right-
|
||||
;; justified in 4.
|
||||
(defun receipt (item price)
|
||||
(format nil "~a ~a" item price))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal "hi" (add-line "hi"))
|
||||
(check-equal "parrot" (add-line "parrot"))
|
||||
(check-equal " ada" (right-column "ada" 6))
|
||||
(check-equal "hi" (right-column "hi" 2))
|
||||
(check-equal "tea 2" (receipt "tea" 2))
|
||||
(check-equal "coffee 3" (receipt "coffee" 3))
|
||||
Reference in new issue
Block a user