![]() |
VOOZH | about |
Pre-requisites: Introduction to LISP
Lisp provides a huge set of facilities for performing input/output. All the input/output operations are performed on streams of several kinds. While reading and writing binary data is possible, the majority of Common Lisp input/output methods read or write characters. For reading and writing individual characters or lines of data, there are straightforward primitives. However, reading and writing written representations of any arbitrary Lisp objects is the most helpful input/output operation.
Lisp Objects:
Lisp objects are typically intricate data structures rather than text strings. They differ from text strings in terms of attributes because of how they are internally represented. However, Lisp offers a representation of the majority of objects in the form of written text, known as the printed representation, which is used for I/O facilities. This makes it possible to access and discuss Lisp objects.
The characters of a Lisp object's printed representation are transmitted to a stream via functions like print. The (Lisp) printer is a group of functions that accomplishes this. The (Lisp) reader is a group of routines that performs the read function, which accepts characters from a stream, interprets them as a printed representation of a Lisp object, creates that object, and returns it.
The input functions are split into two categories: those that work with character streams and those that work with binary streams.
There are many optional arguments in character input functions called input-stream, eof-error-p, and eof-value. The input-stream argument is the stream from which to collect input; if not specified or nil it defaults to the value of the special variable 'standard-input'. The eof-error-p parameter controls what happens if the input is from a file and the end of the file is reached. If eof-error-p is true which is the default, an error will be signaled at end of the file. If it is false, then no error is encountered, and instead, the function returns eof-value.
read &optional input-stream eof-error-p eof-value recursive-p
read-line &optional input-stream eof-error-p eof-value recursive-p
read-char &optional input-stream eof-error-p eof-value recursive-p
unread-char character &optional input-stream
read-preserving-whitespace &optional in-stream eof-error-p eof-value recursive-p
read-delimited-list char &optional input-stream recursive-p
peek-char &optional peek-type input-stream eof-error-p eof-value recursive-p
listen &optional input-stream
read-char-no-hang &optional input-stream eof-error-p eof-value recursive-p
clear-input &optional input-stream
read-from-string string &optional eof-error-p eof-value &key :start :end :preserve-whitespace
parse-integer string &key :start :end :radix :junk-allowed
Example :
Output :
read-byte binary-input-stream &optional eof-error-p eof-value
The read function in Common Lisp allows for keyboard input. It may not take any parameter. Characters are read from an input stream and parsed into representations of Lisp objects.
Syntax:
(setq varname (read))
This statement will take input from the keyboard and store it in the variable.
Example:
Output:
The output functions are split into two categories: those that work with character streams and those that work with binary streams. Due to its extreme complexity, the function format—which operates on streams of characters—is discussed in a section separate from that of the other character-output functions.
Each of these routines accepts an optional argument called output-stream that specifies where the output should be sent. Output-stream defaults to the value of the variable "standard-output" if unspecified or nil. The value of the variable "terminal-io" is used if the value is t.
write object &key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array
write object &key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatch
prin1 object &optional output-stream
print object &optional output-stream
pprint object &optional output-stream
princ object &optional output-stream
write-to-string object &key :escape :radix :base :circle :pretty :level :length :case :gensym :array
prin1-to-string object
princ-to-string object
write-char character &optional output-stream
write-string string & optional output-stream & key :start :end
terpri &optional output-stream
fresh-line &optional output-stream
finish-output &optional output-stream
force-output &optional output-stream
clear-output &optional output-stream
Example :
Output :
write-byte integer binary-output-stream
The function format is highly helpful for creating well-formatted text, attractive messages, and other things. A stream or a string can be produced by format.
Syntax:
format destination control-string &rest arguments
The standard output is the syntax's destination, while the control-string variable holds the characters that will be output along with the printing instruction.
With the exception of the tilde (~), which begins a directive, the format outputs the characters of the control string. It is specified by the character following the tilde, which may be followed by prefix parameters and modifiers. A tilde (~), optional prefix parameters separated by commas, an optional colon (:) and at-sign (@) modifiers, and a single character designating the type of directive this is make up a format directive.
Typically, the prefix parameters are integers with optionally signed decimal notation.
The following table tabulates some of the commonly used directives with their descriptions:
| Directive | Description |
|---|---|
| ~A | followed by ASCII arguments. |
| ~S | followed by S-expressions. |
| ~B | For binary arguments. |
| ~C | For character arguments. |
| ~D | For decimal arguments. |
| ~E | Exponential floating-point arguments. |
| ~F | For Fixed-format floating-point arguments. |
| ~O | For octal arguments. |
| ~X | For hexadecimal arguments. |
| ~$ | Dollar and floating point arguments. |
| ~% | A new line is printed. |
| ~* | The next argument is ignored. |
| ~? | Indirection. The next argument must be a string, and the one after it a list. |
| ~T | Tabulate. This spaces over to a given column. |
Example :
Output :