Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table

This commit is contained in:
venomade committed 2026-11-22 11:15:35 +00:00
1 parent 77843402fa
commit 4675bd1106
76 files changed
+2505 -14

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
;;; FILES 1: WITH-OPEN-FILE (solution)
(defparameter *note-file* "lisplings-note.txt")
(defun save-note ()
(with-open-file (out *note-file* :direction :output :if-exists :supersede)
(format out "(lisp is lovely)~%")
t))
(defun load-note ()
(with-open-file (in *note-file*)
(read in)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-note))
(check-equal '(lisp is lovely) (load-note))
+30
View File
@@ -0,0 +1,30 @@
;;; FILES 2: READING LINES (solution)
(defparameter *poem-file* "lisplings-poem.txt")
(defun save-poem ()
(with-open-file (out *poem-file* :direction :output :if-exists :supersede)
(format out "(lisp is life)~%")
(format out "lambda forever~%")
(format out "close the parens~%"))
t)
(defun poem-lines ()
(with-open-file (in *poem-file*)
(poem-rest in)))
(defun poem-rest (in)
(let ((line (read-line in nil nil)))
(if line
(cons line (poem-rest in))
nil)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-poem))
(check-equal '("(lisp is life)" "lambda forever" "close the parens")
(poem-lines))
(check-equal 3 (length (poem-lines)))
+22
View File
@@ -0,0 +1,22 @@
;;; FILES 3: PRINTING TO STREAMS (solution)
(defun two-views (x)
(with-output-to-string (s)
(prin1 x s)
(princ " " s)
(princ x s)))
(defun name-tag (name)
(with-output-to-string (s)
(print "name:" s)
(princ name s)))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "\"hi\" hi" (two-views "hi"))
(check-equal "7 7" (two-views 7))
(check-equal "
\"name:\" ada" (name-tag "ada"))
+34
View File
@@ -0,0 +1,34 @@
;;; FILES 4: NUMBERS FROM FILES (solution)
(defparameter *count-file* "lisplings-counts.txt")
(defun save-counts ()
(with-open-file (out *count-file* :direction :output :if-exists :supersede)
(format out "10~%")
(format out "20~%")
(format out "5~%"))
t)
(defun sum-counts ()
(with-open-file (in *count-file*)
(sum-rest in)))
(defun sum-rest (in)
(let ((line (read-line in nil nil)))
(if line
(+ (parse-integer line) (sum-rest in))
0)))
(defun first-number (s)
(parse-integer s :junk-allowed t))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-counts))
(check-equal 35 (sum-counts))
(check-equal 12 (first-number "12 apples"))
(check-equal nil (first-number "no digits here"))
(check-error (parse-integer "12 apples") 'parse-error)