Input and Output

As with all programming languages, Scheme has ways of communicating to users and to peripherals. The general abstraction used for I/O is the port, which works similarly to a stream in C. A given port can be either an input port or an output port, but not both. Ports can be opened using (open-input-file) or (open-output-file), and closed with (close-input-port) and (close-output-port).

The most commonly used output function is (display), which sends a message to a given port (or the current port if the second argument is omitted); (display) can print a literal value for any primitive type of atom or list. (read) can be used to read from an input port, and (write) can write to an output port. The (read-char) and (write-char) functions, not surprisingly, read and write single characters to the given port; (peek-char) can be used to read in a character without removing it from the input stream. The (newline) function can be used to end a line of text.

Contents Previous Next