LISP - Interactive I/O
Lisp - Modern Interactive and File I/O
Section titled “Lisp - Modern Interactive and File I/O”Interactive I/O is at the heart of the Lisp development experience, epitomized by the REPL (Read-Eval-Print Loop). This chapter covers the modern approach to handling I/O, from interacting with the user to robustly managing files.
The REPL: Your Interactive Gateway
Section titled “The REPL: Your Interactive Gateway”The REPL is more than just a command line; it’s a powerful, dynamic development environment. The typical workflow is:
- Read: The REPL reads a Lisp expression you type.
- Eval: It evaluates (executes) that expression.
- Print: It prints the result of the evaluation back to you.
- Loop: It waits for your next input.
Modern Lisp environments like SLIME (for Emacs) or SLY (for Emacs/VS Code) supercharge the REPL, allowing you to compile files, inspect objects, and debug code interactively.
Core Concepts: Streams
Section titled “Core Concepts: Streams”A stream is the fundamental abstraction for I/O in Lisp. It represents a source or destination for data. Common Lisp provides several standard streams for convenience:
*standard-input*: The default input stream, typically connected to the keyboard.*standard-output*: The default output stream, typically the terminal display.*error-output*: The stream for error messages, usually also the terminal.*terminal-io*: A special bidirectional stream for direct interaction with the user.
Key I/O Functions
Section titled “Key I/O Functions”Reading Input
Section titled “Reading Input”read: Reads a full Lisp expression (an S-expression) from a stream. It parses the text into a Lisp object.read-line: Reads a line of text as a string, consuming the newline character.read-char: Reads a single character from a stream.
Writing Output
Section titled “Writing Output”print: Prints a Lisp object preceded by a newline and followed by a space. Designed to be machine-readable (can be read back withread).prin1: Likeprint, but without the newline and space.princ: Prints an object in a more human-readable format (e.g., strings without quotes).write: A highly configurable function for printing objects.format: The most powerful output function. It creates formatted strings using control directives.
Mastering format
Section titled “Mastering format”format is a versatile tool for producing structured text. Its first argument is the destination stream (t for *standard-output*, nil to return a string), followed by a control string and arguments.
;; Example of using `format`
(defparameter *user-name* "Alex")(defparameter *items-count* 5)
;; Print to standard output(format t "Hello, ~a! You have ~d items.~%" *user-name* *items-count*)
;; Directives used:;; ~a : Aesthetic (human-readable) representation;; ~d : Decimal integer;; ~% : Newline
;; Create a formatted string by using nil as the destination(defparameter *message* (format nil "Report for user: ~s" *user-name*))
(print *message*);; ~s : Standard (machine-readable) representationOutput
Section titled “Output”Hello, Alex! You have 5 items."Report for user: \"Alex\""Modern File I/O: with-open-file
Section titled “Modern File I/O: with-open-file”While open and close exist, the modern and safe way to work with files is the with-open-file macro. It automatically handles closing the file stream, even if an error occurs within its body. This prevents resource leaks.
(with-open-file (stream-variable filespec options*) ;; code that uses stream-variable )Example: Writing to and Reading from a File
Section titled “Example: Writing to and Reading from a File”;; --- Writing to a file ---(with-open-file (out "/tmp/my-data.txt" :direction :output :if-exists :supersede) ; Overwrite if file exists (format out "This is line 1.~%") (format out "This is line 2.~%"))
(format t "File 'my-data.txt' written successfully.~%~%")
;; --- Reading from the file ---(format t "Contents of 'my-data.txt':~%")(with-open-file (in "/tmp/my-data.txt" :direction :input) (loop for line = (read-line in nil) ; Read line, return nil at EOF while line do (format t "> ~a~%" line)))Output
Section titled “Output”File 'my-data.txt' written successfully.
Contents of 'my-data.txt':> This is line 1.> This is line 2.Further Steps: Project Management
Section titled “Further Steps: Project Management”For real projects, you won’t just type in a REPL. You’ll organize your code into files and projects. The standard tools for this are:
- ASDF (Another System Definition Facility): The de facto build system for Common Lisp. You define a
.asdfile to specify which files are part of your project (or “system”). - Quicklisp: The de facto library manager for Common Lisp. It allows you to easily download, install, and load thousands of open-source libraries.