Files
lisplings/solutions/21_files/files2.lisp
T

31 lines
887 B
Common Lisp

;;; 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)))