Next: , Previous: , Up: Input and output   [Contents][Index]


6.6.2 Input

 






library procedure: read
library procedure: read port

Read’ converts external representations of Scheme objects into the objects themselves. That is, it is a parser for the nonterminal <datum> (see sections see External representation and see Pairs and lists). ‘Read’ returns the next object parsable from the given input port, updating port to point to the first character past the end of the external representation of the object.

If an end of file is encountered in the input before any characters are found that can begin an object, then an end of file object is returned. The port remains open, and further attempts to read will also return an end of file object. If an end of file is encountered after the beginning of an object’s external representation, but the external representation is incomplete and therefore not parsable, an error is signalled.

The port argument may be omitted, in which case it defaults to the value returned by ‘current-input-port’. It is an error to read from a closed port.

procedure: read-char
procedure: read-char port

Returns the next character available from the input port, updating the port to point to the following character. If no more characters are available, an end of file object is returned. Port may be omitted, in which case it defaults to the value returned by ‘current-input-port’.

procedure: peek-char
procedure: peek-char port

Returns the next character available from the input port, without updating the port to point to the following character. If no more characters are available, an end of file object is returned. Port may be omitted, in which case it defaults to the value returned by ‘current-input-port’.

Note: The value returned by a call to ‘peek-char’ is the same as the value that would have been returned by a call to ‘read-char’ with the same port. The only difference is that the very next call to ‘read-char’ or ‘peek-char’ on that port will return the value returned by the preceding call to ‘peek-char’. In particular, a call to ‘peek-char’ on an interactive port will hang waiting for input whenever a call to ‘read-char’ would have hung.

procedure: eof-object? obj

Returns #t if obj is an end of file object, otherwise returns #f. The precise set of end of file objects will vary among implementations, but in any case no end of file object will ever be an object that can be read in using ‘read’.

procedure: char-ready?
procedure: char-ready? port

Returns #t if a character is ready on the input port and returns #f otherwise. If ‘char-ready’ returns #t then the next ‘read-char’ operation on the given port is guaranteed not to hang. If the port is at end of file then ‘char-ready?’ returns #t. Port may be omitted, in which case it defaults to the value returned by ‘current-input-port’.

Rationale:Char-ready?’ exists to make it possible for a program to accept characters from interactive ports without getting stuck waiting for input. Any input editors associated with such ports must ensure that characters whose existence has been asserted by ‘char-ready?’ cannot be rubbed out. If ‘char-ready?’ were to return #f at end of file, a port at end of file would be indistinguishable from an interactive port that has no ready characters.


Next: , Previous: , Up: Input and output   [Contents][Index]