Input/Output
CHARACTERSTRINGS
strings are not symbols
stringp
format
~% new line
~& new line ,unless already new line
> (format t "Fruit flies~%~%like bananas.")
Fruit flies
like bananas.
NIL
~S
(defun square-talk (n)
(format t "~&~S squared is ~S" n (* n n)))
> (square-talk 10)
10 squared is 100
NIL
~A
(defun test (x)
(format t "~&With escape characters: ~S" x)
(format t "~&Without escape characters: ~A" x))
> (test "Hi, mom")
With escape characters: "Hi, mom"
Without escape characters: Hi, mom
NIL
~D
~F
PARAMETERS TO FORMAT DIRECTIVES
the ~S directive accepts a width parameter
read
The object does not have to be quoted becauseit will not be evaluated
(defun my-square ()
(format t "Please type in a number: ")
(let ((x (read)))
(format t "The number ~S squared is ~S.~%"
x (* x x))))
YES-OR-NO-P Y-OR-N-P
(defun riddle ()
(if (yes-or-no-p
"Do you seek Zen enlightenment? ")
(format t "Then do not ask for it!")
(format t "You have found it.")))
READING FILES WITH WITH-OPEN-FILE
(WITH-OPEN-FILE (var pathname)
body)
WITH-OPEN-FILE creates a local variable (just like LET) and setsit to a stream object representing a connection to that file.
Streamobjects are a special Lisp datatype fordescribing connections to files
Onleaving the WITH-OPEN-FILE form,the connection to the file is closed automatically
Let’s try an example ofreading data from a file. Suppose the file ‘‘timber.dat’’ in the directory /usr/dst contains these lines:
"The North Slope"
((45 redwood) (12 oak) (43 maple))
100
We can read this data with the following program:
(defun get-tree-data ()
(with-open-file (stream "/usr/dst/timber.dat")
(let* ((tree-loc (read stream))
(tree-table (read stream))
(num-trees (read stream)))
(format t "~&There are ~S trees on ~S."
num-trees tree-loc)
(format t "~&They are: ~S" tree-table))))
WRITING FILES WITH WITH-OPEN-FILE
(defun save-tree-data (tree-loc tree-table
num-trees)
(with-open-file (stream "/usr/dst/timber.newdat"
:direction :output)
(format stream "~S~%" tree-loc)
(format stream "~S~%" tree-table)
(format stream "~S~%" num-trees)))
Lisp Toolkit: DRIBBLE
The DRIBBLE functionrecords part of a Lisp session in a file
HANDLING END-OF-FILE CONDITIONS
program gets to theend of the file, the next READ will generate an end-of-file error, and you’llend up in the debugger. It is possible to tell READto return a special value, called an eof indicator, instead of generating an error on end of file
a good choice for an eof indicator is a freshly generated cons cell
use EQ rather than EQUAL to make sure that exactly that cons cell is returned
(defun read-my-file ()
(with-open-file (stream "/usr/dst/sample-file")
(let ((contents
(read-all-objects stream (list ’$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream)))))