Files
lisplings/exercises/21_files/files3.lisp
T

37 lines
1.2 KiB
Common Lisp

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