Input Functions

The input functions are split into two categories: those that work with character streams and those that work with binary streams.

Input from Character 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 

  • It constructs a corresponding Lisp object based on the written representation of a Lisp object read from the input stream and then returns the object.

read-line &optional input-stream eof-error-p eof-value recursive-p 

  • read-line inserts a line of text that has been separated by a newline. The line is given back as a character string (without the newline character). This function is typically used to obtain a line of user input. A flag that is true if the end-of-file terminated the (non-empty) line or false if the line was terminated properly is the second value that is returned.

read-char &optional input-stream eof-error-p eof-value recursive-p 

  • One character is taken from the input stream and returned as a character object by read-char.

unread-char character &optional input-stream 

  • The character is moved to the front of the input stream by unread-char. The character must match the one that was just read from the input stream. When a character is next read from the input stream, it will be the specified character followed by the previous contents of the input stream because the input stream “backs up” over this character. Unread-char returns back nil.

read-preserving-whitespace &optional in-stream eof-error-p eof-value recursive-p

  • When it is necessary to know precisely which character ended the extended token, the function read-preserving-whitespace is offered.

read-delimited-list char &optional input-stream recursive-p

  • This reads objects from the stream until the following character is a char (while disregarding whitespace and comments). It returns a list of the read items.

peek-char &optional peek-type input-stream eof-error-p eof-value recursive-p 

  • The peek-type, which has a default value of nil, determines what peek-char performs. Without actually deleting the character from the input stream, peek-char returns the next character to be read from the input stream when the peek-type is nil. The character will remain when input from the input stream is performed again later. It appears as though read-char and unread-char were called back-to-back.

listen &optional input-stream 

  • When a character is instantly available from the input stream, the predicate listen is true; otherwise, it is false. This is especially helpful when the stream gets its characters from a keyboard or other interactive device.

read-char-no-hang &optional input-stream eof-error-p eof-value recursive-p 

  • It is similar to read-char, but instead of waiting for a character if it doesn’t receive one, it returns nil right away.

clear-input &optional input-stream

  • By doing this, the input-related stream’s buffered input is cleared. When an asynchronous error has occurred, it is most useful for clearing type-ahead from keyboards. If the stream in question doesn’t make sense for this operation, clear-input has no effect. Clearing input yields nil.

read-from-string string &optional eof-error-p eof-value &key :start :end :preserve-whitespace

  • It generates a LISP object out of the string’s characters one at a time, then returns the object. Additionally, it returns the length of the string (or length + 1), or the index of the first character in the string that was not read.

parse-integer string &key :start :end :radix :junk-allowed 

  • It looks at the portion of the string that is start and end-delimited (default to the beginning and end of the string). Whitespace characters are skipped over before an attempt is made to parse an integer.

Example :

Lisp




;LISP- Input 
(with-input-from-string (stream "Geeks for Geeks welcomes you!")
   (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 :

 

Input from Binary Streams:

read-byte binary-input-stream &optional eof-error-p eof-value 

  • read-byte extracts one byte from the binary input stream and returns it as an integer.

Reading Input from Keyboard:

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:

Lisp




; Lisp program to demonstrate 
; use of read function
; Program inputs value of radius from
; keyboard and calculates the perimeter of circle
  
(defun PerimOfCircle()
(terpri)
(princ "Enter Radius: ")
(setq radius (read))
(setq perimeter (* 2 3.1416 radius))
(princ "Perimeter: ")
(write perimeter))
(PerimOfCircle)


Output:

 

Input & Output in LISP

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.

Similar Reads

Input Functions:

The input functions are split into two categories: those that work with character streams and those that work with binary streams....

Output Functions:

...

Contact Us