“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs Output Streams “The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
Streams All input and output is accomplished via streams A stream is a something from which data can be read and to which data can be written You’ve likely worked with streams before in other programming languages
Standard Streams There are several streams built in, including: *standard-input* *standard-output* *error-output* *terminal-io* *query-io* *debug-io*
(not (equal #\B #\b)) → T Characters Strings are made up of characters Examples: #\A, #\t, #\Newline Self-quoting: #\A → #\A Every character has an associated integer, usually its ASCII number Case sensitive: (not (equal #\B #\b)) → T
Codes and Chars There are many built-in functions for working with characters char-code returns the number associated with a character code-char returns the character associated with a number char<, char<=, char=, char>=, char> and char/=
Predicates for Characters characterp alpha-char-p digit-char-p alphanumericp upper-case-p lower-case-p both-case-p graphic-char-p standard-char-p
Writing to Streams Plethora of possibilities >(print "Lisp rocks my socks off!" *standard-output*) "Lisp rocks my socks off!" >(write-char #\S *standard-output*) S #\S >(write-string "Hello" *standard-output*) Hello "Hello“ In the above examples, *standard-output* is actually optional because it is the default output stream: >(print "Lisp rocks my socks off!")
Writing From a Progn Block >(progn (print "Hi") (write-string "class")) "Hi" class "class" (terpri) "Hi" class terpri is a carriage return
More Writing Functions prin1 – like print, but no newlines or spaces between calls princ – like prin1, but no double quotes around output strings pprint – like print, but doesn’t return the string printed (also makes output “pretty”) fresh-line – goes to beginning of next line, unless you’re already at the beginning (terpri always goes to next line)
(format destination template S-expr*) Most general and powerful output function (format destination template S-expr*) destination can be a stream, t or nil If t, result is printed to currently selected output device Returns NIL, unless destination is nil, in which case the string produced is returned and no stream is written to
Format (2) Format has a complex set of directives that are close to a programming language in terms of expressive power Format directives are indicated with a ~ Can indicate spacing information and printing specifications Somewhat similar to printf in C
Basic Format Directives Two primary directives: ~S and ~A Not case sensitive ~S prints in a format that LISP can read ~A simply prints “nice” output These will often look the same, but not always
Format Examples >(format t "This is a test") This is a test NIL >(format t "This is a ~A" "test") >(format t "This is a ~S" "test") This is a "test" >(format t "This is an ~S" 'example) This is an EXAMPLE
Other Useful Directives ~~ - prints a ~ ~& - like fresh-line ~% - like terpri ~D - decimal number ~B - binary number ~O - octal number ~X - hexadecimal number ~F - fixed-format floating point number ~E - exponential floating point number ~G - general floating point number ~$ - money format, meaning fixed floating point with two decimals