Common LISP provides numerous input-output functions. We have already used the format function, and print function for output. In this section, we will look into some of the most commonly used input-output functions provided in LISP.
Input Functions
The following table provides the most commonly used input functions of LISP −
Sr.No. | Function & Description |
---|---|
1 | read & optional input-stream eof-error-p eof-value recursive-pIt reads in the printed representation of a Lisp object from input-stream, builds a corresponding Lisp object, and returns the object. |
2 | read-preserving-whitespace & optional in-stream eof-error-p eof-value recursive-pIt is used in some specialized situations where it is desirable to determine precisely what character terminated the extended token. |
3 | read-line & optional input-stream eof-error-p eof-value recursive-pIt reads in a line of text terminated by a newline. |
4 | read-char & optional input-stream eof-error-p eof-value recursive-pIt takes one character from input-stream and returns it as a character object. |
5 | unread-char character & optional input-streamIt puts the character most recently read from the input-stream, onto the front of input-stream. |
6 | peek-char & optional peek-type input-stream eof-error-p eof-value recursive-pIt returns the next character to be read from input-stream, without actually removing it from the input stream. |
7 | listen & optional input-streamThe predicate listen is true if there is a character immediately available from input-stream, and is false if not. |
8 | read-char-no-hang & optional input-stream eof-error-p eof-value recursive-pIt is similar to read-char, but if it does not get a character, it does not wait for a character, but returns nil immediately. |
9 | clear-input & optional input-streamIt clears any buffered input associated with input-stream. |
10 | read-from-string string & optional eof-error-p eof-value & key :start :end :preserve-whitespaceIt takes the characters of the string successively and builds a LISP object and returns the object. It also returns the index of the first character in the string not read, or the length of the string (or, length +1), as the case may be. |
11 | parse-integer string & key :start :end :radix :junk-allowedIt examines the substring of string delimited by :start and :end (default to the beginning and end of the string). It skips over whitespace characters and then attempts to parse an integer. |
12 | read-byte binary-input-stream & optional eof-error-p eof-valueIt reads one byte from the binary-input-stream and returns it in the form of an integer. |
Reading Input from Keyboard
The read function is used for taking input from the keyboard. It may not take any argument.
For example, consider the code snippet −
(write ( + 15.0 (read)))
Assume the user enters 10.2 from the STDIN Input, it returns,
25.2
The read function reads characters from an input stream and interprets them by parsing as representations of Lisp objects.
Example
Create a new source code file named main.lisp and type the following code in it −
; the function AreaOfCircle
; calculates area of a circle
; when the radius is input from keyboard
(defun AreaOfCircle()
(terpri)
(princ "Enter Radius: ")
(setq radius (read))
(setq area (* 3.1416 radius radius))
(princ "Area: ")
(write area))
(AreaOfCircle)
When you execute the code, it returns the following result −
Enter Radius: 5 (STDIN Input)
Area: 78.53999
Example
Create a new source code file named main.lisp and type the following code in it.
(with-input-from-string (stream "Welcome to Tutorials Point!")
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (peek-char nil stream nil 'the-end))
(values)
)
When you execute the code, it returns the following result −
#\W
#\e
#\l
#\c
#\o
#\m
#\e
#\Space
#\t
#\o
#\Space
The Output Functions
All output functions in LISP take an optional argument called output-stream, where the output is sent. If not mentioned or nil, output-stream defaults to the value of the variable *standard-output*.
The following table provides the most commonly used output functions of LISP −
Sr.No. | Function and Description |
---|---|
1 | write object & key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :arraywrite object & key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatchBoth write the object to the output stream specified by :stream, which defaults to the value of *standard-output*. Other values default to the corresponding global variables set for printing. |
2 | prin1 object & optional output-streamprint object & optional output-streampprint object & optional output-streamprinc object & optional output-streamAll these functions outputs the printed representation of object to output-stream. However, the following differences are there −prin1 returns the object as its value.print prints the object with a preceding newline and followed by a space. It returns object.pprint is just like print except that the trailing space is omitted.princ is just like prin1 except that the output has no escape character |
3 | write-to-string object & key :escape :radix :base :circle :pretty :level :length :case :gensym :arraywrite-to-string object & key :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatchprin1-to-string objectprinc-to-string objectThe object is effectively printed and the output characters are made into a string, which is returned. |
4 | write-char character & optional output-streamIt outputs the character to output-stream, and returns character. |
5 | write-string string & optional output-stream & key :start :endIt writes the characters of the specified substring of string to the output-stream. |
6 | write-line string & optional output-stream & key :start :endIt works the same way as write-string, but outputs a newline afterwards. |
7 | terpri & optional output-streamIt outputs a newline to output-stream. |
8 | fresh-line & optional output-streamit outputs a newline only if the stream is not already at the start of a line. |
9 | finish-output & optional output-streamforce-output & optional output-streamclear-output & optional output-streamThe function finish-output attempts to ensure that all output sent to output-stream has reached its destination, and only then returns nil.The function force-output initiates the emptying of any internal buffers but returns nil without waiting for completion or acknowledgment.The function clear-output attempts to abort any outstanding output operation in progress in order to allow as little output as possible to continue to the destination. |
10 | write-byte integer binary-output-streamIt writes one byte, the value of the integer. |
Example
Create a new source code file named main.lisp and type the following code in it.
; this program inputs a numbers and doubles it
(defun DoubleNumber()
(terpri)
(princ "Enter Number : ")
(setq n1 (read))
(setq doubled (* 2.0 n1))
(princ "The Number: ")
(write n1)
(terpri)
(princ "The Number Doubled: ")
(write doubled)
)
(DoubleNumber)
When you execute the code, it returns the following result −
Enter Number : 3456.78 (STDIN Input)
The Number: 3456.78
The Number Doubled: 6913.56
Formatted Output
The function format is used for producing nicely formatted text. It has the following syntax −
format destination control-string &rest arguments
where,
- destination is standard output
- control-string holds the characters to be output and the printing directive.
A format directive consists of a tilde (~), optional prefix parameters separated by commas, optional colon (:) and at-sign (@) modifiers, and a single character indicating what kind of directive this is.
The prefix parameters are generally integers, notated as optionally signed decimal numbers.
The following table provides brief description of the commonly used directives −
Sr.No. | Directive & Description |
---|---|
1 | ~AIs followed by ASCII arguments. |
2 | ~SIs followed by S-expressions. |
3 | ~DFor decimal arguments. |
4 | ~BFor binary arguments. |
5 | ~OFor octal arguments. |
6 | ~XFor hexadecimal arguments. |
7 | ~CFor character arguments. |
8 | ~FFor Fixed-format floating-point arguments. |
9 | ~EExponential floating-point arguments. |
10 | ~$Dollar and floating point arguments. |
11 | ~%A new line is printed. |
12 | ~*Next argument is ignored. |
13 | ~?Indirection. The next argument must be a string, and the one after it a list. |
Example
Let us rewrite the program calculating a circle’s area −
Create a new source code file named main.lisp and type the following code in it.
(defun AreaOfCircle()
(terpri)
(princ "Enter Radius: ")
(setq radius (read))
(setq area (* 3.1416 radius radius))
(format t "Radius: = ~F~% Area = ~F" radius area)
)
(AreaOfCircle)
When you execute the code, it returns the following result −
Enter Radius: 10.234 (STDIN Input)
Radius: = 10.234
Area = 329.03473
Leave a Reply