
- LISP Tutorial
- LISP - Home
- LISP - Overview
- LISP - Environment
- LISP - REPL
- LISP - Program Structure
- LISP - Basic Syntax
- LISP - Data Types
- Lisp Macros
- LISP - Macros
- LISP - Backquote and Comma
- LISP - Code Generation Using Macro
- LISP - Variable Capture and Hygienic macro
- LISP - Scope and Binding
- LISP - Macro Writing Style
- LISP - Macro Characters
- LISP - Read-Time Macros
- LISP - Compiler Macros
- LISP - Uses of Macros
- Lisp Functions
- LISP - Functions
- LISP - Functions vs Macros
- LISP - Calling Function using funcall
- LISP - Calling Function using apply
- LISP - Closures
- LISP - Functions as Arguments
- LISP - Functions as Return Values
- LISP - Recursion
- LISP - Built-in Functions
- Lisp Predicates
- LISP - Predicates
- LISP - Generic Data Type Predicates
- LISP - Specific Data Type Predicates
- LISP - Equality Predicates
- LISP - Numeric Predicates
- LISP - Comparison Predicates
- LISP - Logical Predicates
- LISP - List Predicates
- LISP - Custom Predicates
- LISP - Chaining Predicates
- Lisp Arrays
- LISP - Arrays
- LISP - Adjustable Arrays
- LISP - Fill Pointers in Arrays
- LISP - Specialized Arrays
- LISP - Arrays Properties
- LISP - Iterating over Arrays
- LISP - Multidimensional Arrays
- LISP - Row-Major Order
- Lisp Strings
- LISP - Strings
- LISP - String Concatenation
- LISP - String Comparison
- LISP - String Case Conversion
- LISP - String Trimmimg
- LISP - String Searching
- LISP - Getting Substring
- LISP - String Replacement
- LISP - Sorting Strings
- LISP - Merging Strings
- LISP - Accessing Characters of String
- LISP - String length
- LISP - Escape Sequences
- Lisp Sequences
- LISP - Sequences
- LISP - Accessing Element of Sequence
- LISP - Sequence length
- LISP - Getting Subsequence
- LISP - Search Element in Sequence
- LISP - Sequence Concatenation
- LISP - Reversing a Sequence
- LISP - Mapping Sequence Element
- LISP - position of Element
- LISP - Remove an Element
- LISP - Sort Sequence
- LISP - Merge Sequences
- LISP - every function
- LISP - some function
- LISP - notany function
- LISP - notevery function
- Lisp Lists
- LISP - Lists
- LISP - Accessing Elements of Lists
- LISP - Modifications to Lists
- LISP - Using mapcar on List
- LISP - Using mapc on List
- LISP - Using reduce on List
- LISP - Removing elements from List
- LISP - Reversing a List
- LISP - Sorting a List
- LISP - Searching a List
- LISP - List vs Vectors
- LISP - Matrix Multiplication
- Lisp Vectors
- LISP - Vectors
- LISP - Creating Vectors
- LISP - Accessing Elements of Vectors
- LISP - Modifications to Vectors
- LISP - Adjustable Vectors
- LISP - Specialized Vectors
- LISP - Vector Functions
- Lisp Set
- LISP - Set
- LISP - Adding elements to the Set
- LISP - Getting SubSet from a Set
- LISP - Set Difference
- LISP - Set Exclusive OR
- LISP - Set Intersection
- LISP - Set Union
- LISP - Representing Set with HashTable
- LISP - List as Set vs HashTable as Set
- Lisp Tree
- LISP - Tree
- LISP - Recursive Traversal
- LISP - Inorder Traversal
- LISP - Preorder Traversal
- LISP - Postorder Traversal
- LISP - Depth First Traversal
- LISP - Modifying Tree
- LISP - Search Tree
- LISP - Binary Tree
- Lisp Hash Table
- LISP - Hash Table
- Adding Values to Hash Table
- Removing Values from Hash Table
- Updating Values of Hash Table
- Iterating Hash Table Entries
- Searching key in HashTable
- Checking Size of HashTable
- Using Custom Equality Check
- Lisp - Input − Output
- LISP - Input − Output
- LISP - Streams
- LISP - Reading Data from Streams
- LISP - Writing Data to Streams
- LISP - File I/O
- LISP - String I/O
- LISP - Formatting with Format
- LISP - Interactive I/O
- LISP - Error Handling
- LISP - Binary I/O
- Lisp - Structures
- LISP - Structures
- LISP - Accessors and Mutators
- LISP - Structure Options
- LISP - Structure Types
- LISP - Applications and Best Practices
- Lisp - CLOS
- LISP - CLOS
- Lisp - Objects
- LISP - Class
- LISP - Slots and Accessors
- LISP - Generic Functions
- LISP - Class Precedence
- LISP - Metaobject Protocol
- LISP - Multimethods
- LISP - Multiple Inheritance
- LISP - Method Combinations
- LISP - Method Combinations
- LISP - :before Method Combination
- LISP - :primary Method Combination
- LISP - :after Method Combination
- LISP - :around Method Combination
- LISP - + Method Combination
- LISP - and Method Combination
- LISP - append Method Combination
- LISP Useful Resources
- Lisp - Quick Guide
- Lisp - Useful Resources
- Lisp - Discussion
Lisp - Input & Output
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-p It 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-p It 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-p It reads in a line of text terminated by a newline. |
4 |
read-char & optional input-stream eof-error-p eof-value recursive-p It takes one character from input-stream and returns it as a character object. |
5 |
unread-char character & optional input-stream It 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-p It returns the next character to be read from input-stream, without actually removing it from the input stream. |
7 |
listen & optional input-stream The 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-p It 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-stream It clears any buffered input associated with input-stream. |
10 |
read-from-string string & optional eof-error-p eof-value & key :start :end :preserve-whitespace It 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-allowed It 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-value It 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 −
main.lisp
(setq a ( + 15.0 (read))) (write a)
Output
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
Update the source code file named main.lisp and type the following code in it −
main.lisp
; 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)) (print "Area: ") (write area)) (AreaOfCircle)
Output
When you execute the code, it returns the following result −
Enter Radius: 5 (STDIN Input) Area: 78.53999
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
(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) )
Output
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 :array write object & key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatch Both 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-stream print object & optional output-stream pprint object & optional output-stream princ object & optional output-stream All these functions outputs the printed representation of object to output-stream. However, the following differences are there −
|
3 |
write-to-string object & key :escape :radix :base :circle :pretty :level :length :case :gensym :array write-to-string object & key :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatch prin1-to-string object princ-to-string object The object is effectively printed and the output characters are made into a string, which is returned. |
4 |
write-char character & optional output-stream It outputs the character to output-stream, and returns character. |
5 |
write-string string & optional output-stream & key :start :end It writes the characters of the specified substring of string to the output-stream. |
6 |
write-line string & optional output-stream & key :start :end It works the same way as write-string, but outputs a newline afterwards. |
7 |
terpri & optional output-stream It outputs a newline to output-stream. |
8 |
fresh-line & optional output-stream it outputs a newline only if the stream is not already at the start of a line. |
9 |
finish-output & optional output-stream force-output & optional output-stream clear-output & optional output-stream
|
10 |
write-byte integer binary-output-stream It writes one byte, the value of the integer. |
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; this program inputs a numbers and doubles it (defun DoubleNumber() (terpri) (print "Enter Number : ") (setq n1 (read)) (setq doubled (* 2.0 n1)) (print "The Number: ") (write n1) (terpri) (print "The Number Doubled: ") (write doubled) ) (DoubleNumber)
Output
When you execute the code, it returns the following result −
"Enter Number : " 3456.78 "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 |
~A Is followed by ASCII arguments. |
2 |
~S Is followed by S-expressions. |
3 |
~D For decimal arguments. |
4 |
~B For binary arguments. |
5 |
~O For octal arguments. |
6 |
~X For hexadecimal arguments. |
7 |
~C For character arguments. |
8 |
~F For Fixed-format floating-point arguments. |
9 |
~E Exponential 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 −
Update the source code file named main.lisp and type the following code in it.
main.lisp
(defun AreaOfCircle() (terpri) (princ "Enter Radius: ") (setq radius (read)) (setq area (* 3.1416 radius radius)) (format t "Radius: = ~F~% Area = ~F" radius area) ) (AreaOfCircle)
Output
When you execute the code, it returns the following result −
Enter Radius: 10.234 (STDIN Input) Radius: = 10.234 Area = 329.03473