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

+6
View File
@@ -0,0 +1,6 @@
# 21_files
Files and streams: write, read back, parse.
WITH-OPEN-FILE closes for you; READ-LINE walks lines; PARSE-INTEGER
turns text into numbers.
+36
View File
@@ -0,0 +1,36 @@
;;; FILES 1: WITH-OPEN-FILE
;;;
;;; (with-open-file (stream path :direction :output) body) opens a
;;; file, runs the body, and closes the file afterwards - even when
;;; the body signals an error. :DIRECTION decides which way the
;;; stream flows: :output writes, and the default is :input.
;;;
;;; FORMAT's first argument can be a stream instead of NIL: the
;;; output lands in the file instead of a string.
;;;
;;; (format nil "...") => a string
;;; (format out "...") => into the file
;;;
;;; READ pulls ONE s-expression off a stream: the same reader that
;;; reads your code, reading the file's text instead.
(defparameter *note-file* "lisplings-note.txt")
;; TODO: Write the list (lisp is lovely) to *NOTE-FILE*, then
;; return T so the check below can see the save worked.
(defun save-note ()
(with-open-file (out *note-file*)
(format out "(lisp is lovely)~%")
t))
;; TODO: READ the list back out of *NOTE-FILE*.
(defun load-note ()
(read *note-file*))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check (save-note))
(check-equal '(lisp is lovely) (load-note))
+41
View File
@@ -0,0 +1,41 @@
;;; FILES 2: READING LINES
;;;
;;; (read-line in) returns the next line as a string. At the end of
;;; the file it signals an error - unless you say what to hand back
;;; instead:
;;;
;;; (read-line in nil nil) => the next line, or NIL at the end
;;;
;;; NIL at the end is the base case the recursion chapter taught:
;;; read a line, cons it onto the rest.
;;;
;;; SAVE-POEM below is complete: the exercise writes the very file
;;; it reads, so the checks need nothing from outside.
(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)))
;; TODO: The lines still waiting in the stream IN, in order - NIL
;; once READ-LINE hands you NIL.
(defun poem-rest (in)
(read-line in))
;;; ------------------------------------------------------------------
;;; 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)))
+36
View File
@@ -0,0 +1,36 @@
;;; FILES 3: PRINTING TO STREAMS
;;;
;;; PRIN1 prints readably - what the reader could read back, quotes
;;; included. PRINC prints for humans - no quotes. PRINT is PRIN1
;;; with a newline in front and a space behind. They all take the
;;; stream as their second argument.
;;;
;;; WITH-OUTPUT-TO-STRING makes a stream that collects into a
;;; string, so checks can watch printing without a file:
;;;
;;; (with-output-to-string (s)
;;; (princ "hi" s)) => "hi"
;; TODO: X twice: readable, one space, then plain.
(defun two-views (x)
(with-output-to-string (s)
(princ x s)
(princ " " s)
(princ x s)))
;; TODO: The label "name:" on its own line - PRINT starts the line
;; - then NAME after it, printed plainly.
(defun name-tag (name)
(with-output-to-string (s)
(princ "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"))
+46
View File
@@ -0,0 +1,46 @@
;;; FILES 4: NUMBERS FROM FILES
;;;
;;; A file full of text can be full of numbers wearing strings.
;;; PARSE-INTEGER takes one off:
;;;
;;; (parse-integer "42") => 42
;;;
;;; With :JUNK-ALLOWED T it reads the leading number and ignores
;;; the rest; without it, junk is a PARSE-ERROR. No number at all,
;;; and the junk-allowed answer is NIL.
;;;
;;; SAVE-COUNTS below is complete: the exercise writes the very
;;; file it reads.
(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)))
;; TODO: The lines of IN, each parsed into a number, added up.
(defun sum-rest (in)
(parse-integer (read-line in)))
;; TODO: The leading number in the string S, junk allowed - NIL
;; when S has no number at all.
(defun first-number (s)
(parse-integer s))
;;; ------------------------------------------------------------------
;;; 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)