Lisp - Streams



Common Lisp uses Streams for Input/Output Operations. Streams are fundamenatal units to perform I/O. Let's check the basics of Stream in Common LISP.

Core Concepts

Data Flow

A stream represents flow of data either into a program as input operation or from the program as output operation. A data can be in any form from characters, bytes or even as LISP object.

Abstraction

A stream provides an abstraction layer to access different types of data sources and sinks in a uniform way. A data source or a sink can be Console, File System or Strings etc.

Key Aspects

Funtions for I/O

In LISP, we've read, read-char functions to read from input streams and write, write-char functions to write to output streams.

Standard Streams

Common Lisp provides standard streams for basic I/O on console as following−

  • *standard-input* − Generally connected to keyboard.

  • *standard-output* − Generally connected to display.

File Streams

Common Lisp provides file streams for basic I/O on file system−

  • open − Used to create a file Stream

  • read − read function can be used to read from file stream.

  • write − write function can be used to write to file stream.

String Streams

Common Lisp provides String streams as well which can be used to manipulate strings and to treat them as data source or sink.

Stream Types

Streams can be broadly classify into two types of streams.

  • Character Based − Character streams are used for text based data sources/sinks.

  • Binary Based − Binary streams are useful in byte oriented data handling.

Gray Stream

Gray Stream are extension of Common Lisp providing us a powerful and flexible option to customize streams behavior. Gray streams are used for advanced stream manipulations.

Summary

LISP Streams are fundamental unit to handle I/O. Streams provides a flexible and consistent way in order to work with diverse sources of data. Streams are core part of LISP I/O eco-system and enables a LISP program to interact with external world.

Advertisements