This is r4rs.info, produced by Makeinfo version 3.12a from r4rs.texi. INFO-DIR-SECTION Scheme Programming START-INFO-DIR-ENTRY * r4rs: (r4rs). Revised(4) Report on Scheme END-INFO-DIR-ENTRY  File: r4rs.info, Node: Ports, Next: Input, Prev: Input and output, Up: Input and output Ports ----- Ports represent input and output devices. To Scheme, an input port is a Scheme object that can deliver characters upon command, while an output port is a Scheme object that can accept characters. -- essential procedure: call-with-input-file string proc -- essential procedure: call-with-output-file string proc PROC should be a procedure of one argument, and STRING should be a string naming a file. For `call-with-input-file', the file must already exist; for `call-with-output-file', the effect is unspecified if the file already exists. These procedures call PROC with one argument: the port obtained by opening the named file for input or output. If the file cannot be opened, an error is signalled. If the procedure returns, then the port is closed automatically and the value yielded by the procedure is returned. If the procedure does not return, then the port will not be closed automatically unless it is possible to prove that the port will never again be used for a read or write operation. _Rationale:_ Because Scheme's escape procedures have unlimited extent, it is possible to escape from the current continuation but later to escape back in. If implementations were permitted to close the port on any escape from the current continuation, then it would be impossible to write portable code using both `call-with-current-continuation' and `call-with-input-file' or `call-with-output-file'. -- essential procedure: input-port? obj -- essential procedure: output-port? obj Returns `#t' if OBJ is an input port or output port respectively, otherwise returns `#f'. -- essential procedure: current-input-port -- essential procedure: current-output-port Returns the current default input or output port. -- procedure: with-input-from-file string thunk -- procedure: with-output-to-file string thunk THUNK must be a procedure of no arguments, and STRING must be a string naming a file. For `with-input-from-file', the file must already exist; for `with-output-to-file', the effect is unspecified if the file already exists. The file is opened for input or output, an input or output port connected to it is made the default value returned by `current-input-port' or `current-output-port', and the THUNK is called with no arguments. When the THUNK returns, the port is closed and the previous default is restored. `With-input-from-file' and `with-output-to-file' return the value yielded by THUNK. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent. -- essential procedure: open-input-file filename Takes a string naming an existing file and returns an input port capable of delivering characters from the file. If the file cannot be opened, an error is signalled. -- essential procedure: open-output-file filename Takes a string naming an output file to be created and returns an output port capable of writing characters to a new file by that name. If the file cannot be opened, an error is signalled. If a file with the given name already exists, the effect is unspecified. -- essential procedure: close-input-port port -- essential procedure: close-output-port port Closes the file associated with PORT, rendering the PORT incapable of delivering or accepting characters. These routines have no effect if the file has already been closed. The value returned is unspecified.  File: r4rs.info, Node: Input, Next: Output, Prev: Ports, Up: Input and output Input ----- -- essential procedure: read -- essential procedure: read port `Read' converts external representations of Scheme objects into the objects themselves. That is, it is a parser for the nonterminal (see *Note External representations:: and *Note 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. -- essential procedure: read-char -- essential 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'. -- essential procedure: peek-char -- essential 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. -- essential 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.  File: r4rs.info, Node: Output, Next: System interface, Prev: Input, Up: Input and output Output ------ -- essential procedure: write obj -- essential procedure: write obj port Writes a written representation of OBJ to the given PORT. Strings that appear in the written representation are enclosed in doublequotes, and within those strings backslash and doublequote characters are escaped by backslashes. `Write' returns an unspecified value. The PORT argument may be omitted, in which case it defaults to the value returned by `current-output-port'. -- essential procedure: display obj -- essential procedure: display obj port Writes a representation of OBJ to the given PORT. Strings that appear in the written representation are not enclosed in doublequotes, and no characters are escaped within those strings. Character objects appear in the representation as if written by `write-char' instead of by `write'. `Display' returns an unspecified value. The PORT argument may be omitted, in which case it defaults to the value returned by `current-output-port'. _Rationale:_ `Write' is intended for producing machine-readable output and `display' is for producing human-readable output. Implementations that allow ``slashification'' within symbols will probably want `write' but not `display' to slashify funny characters in symbols. -- essential procedure: newline -- essential procedure: newline port Writes an end of line to PORT. Exactly how this is done differs from one operating system to another. Returns an unspecified value. The PORT argument may be omitted, in which case it defaults to the value returned by `current-output-port'. -- essential procedure: write-char char -- essential procedure: write-char char port Writes the character CHAR (not an external representation of the character) to the given PORT and returns an unspecified value. The PORT argument may be omitted, in which case it defaults to the value returned by `current-output-port'.  File: r4rs.info, Node: System interface, Prev: Output, Up: Input and output System interface ---------------- Questions of system interface generally fall outside of the domain of this report. However, the following operations are important enough to deserve description here. -- essential procedure: load filename FILENAME should be a string naming an existing file containing Scheme source code. The `load' procedure reads expressions and definitions from the file and evaluates them sequentially. It is unspecified whether the results of the expressions are printed. The `load' procedure does not affect the values returned by `current-input-port' and `current-output-port'. `Load' returns an unspecified value. _Rationale:_ For portability, `load' must operate on source files. Its operation on other kinds of files necessarily varies among implementations. -- procedure: transcript-on filename -- procedure: transcript-off FILENAME must be a string naming an output file to be created. The effect of `transcript-on' is to open the named file for output, and to cause a transcript of subsequent interaction between the user and the Scheme system to be written to the file. The transcript is ended by a call to `transcript-off', which closes the transcript file. Only one transcript may be in progress at any time, though some implementations may relax this restriction. The values returned by these procedures are unspecified.  File: r4rs.info, Node: Formal syntax and semantics, Next: Notes, Prev: Standard procedures, Up: Top Formal syntax and semantics *************************** This chapter provides formal descriptions of what has already been described informally in previous chapters of this report. * Menu: * Formal syntax:: * Formal semantics:: * Derived expression types:Formal derived expression types  File: r4rs.info, Node: Formal syntax, Next: Formal semantics, Prev: Formal syntax and semantics, Up: Formal syntax and semantics Formal syntax ============= This section provides a formal syntax for Scheme written in an extended BNF. The syntax for the entire language, including features which are not essential, is given here. All spaces in the grammar are for legibility. Case is insignificant; for example, `#x1A' and `#X1a' are equivalent. stands for the empty string. The following extensions to BNF are used to make the description more concise: * means zero or more occurrences of ; and + means at least one . * Menu: * Lexical structure:: * External representations:External representation * Expression:Expression syntax * Quasiquotations:: * Programs and definitions::  File: r4rs.info, Node: Lexical structure, Next: External representation, Prev: Formal syntax, Up: Formal syntax Lexical structure ----------------- This section describes how individual tokens (identifiers, numbers, etc.) are formed from sequences of characters. The following sections describe how expressions and programs are formed from sequences of tokens. may occur on either side of any token, but not within a token. Tokens which require implicit termination (identifiers, numbers, characters, and dot) may be terminated by any , but not necessarily by anything else. ==> | | | | | ( | ) | #( | ' | `{} | , | ,@ | . ==> | ( | ) | " | ; ==> ==> ; \= ==> | ==> * ==> * | ==> | ==> a | b | c | ... | z ==> ! | \$ | \% | & | * | / | : | < | = | > | ? | ~ | _ | ^ ==> | | ==> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ==> . | + | - ==> + | - | ... ==> | else | => | define | unquote | unquote-splicing ==> quote | lambda | if | set! | begin | cond | and | or | case | let | let* | letrec | do | delay | quasiquote ==> `#t' | `#f' ==> #\ | #\ ==> space | newline ==> " * " ==> | \" | \\ ==> | | | The following rules for , , , , , and should be replicated for R = 2, 8, 10, and 16. There are no rules for , , and , which means that numbers containing decimal points or exponents must be in decimal radix. ==> ==> | | + i | - i | + i | - i | + i | - i | + i | - i ==> ==> | / | ==> | . + #* | + . * #* | + #+ . #* ==> + #* ==> | ==> | + ==> e | s | f | d | l ==> | + | - ==> | #i | #e ==> #b ==> #o ==> | #d ==> #x ==> 0 | 1 ==> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 ==> ==> | a | b | c | d | e | f  File: r4rs.info, Node: External representation, Next: Expression syntax, Prev: Lexical structure, Up: Formal syntax External representation ----------------------- is what the `read' procedure (*Note Input::) successfully parses. Note that any string that parses as an will also parse as a . ==> | ==> | | | | ==> ==> | ==> (*) | (+ . ) | ==> ==> ' | ` | , | ,@ ==> #(*)  File: r4rs.info, Node: Expression syntax, Next: Quasiquotations, Prev: External representation, Up: Formal syntax Expressions ----------- ==> | | | | | | ==> | ==> | | | ==> ' | (quote ) ==> ( *) ==> ==> ==> (lambda ) ==> (*) | | (+ . ) ==> * ==> * ==> ==> (if ) ==> ==> ==> | ==> (set! ) ==> (cond +) | (cond * (else )) | (case +) | (case * (else )) | (and *) | (or *) | (let (*) ) | (let (*) ) | (let* (*) ) | (letrec (*) ) | (begin ) | (do (*) ( ) *) | (delay ) | ==> ( ) | () | ( => ) ==> ==> ((*) ) ==> ( ) ==> ( ) | ( ) ==> ==>  File: r4rs.info, Node: Quasiquotations, Next: Programs and definitions, Prev: Expression syntax, Up: Formal syntax Quasiquotations --------------- The following grammar for quasiquote expressions is not context-free. It is presented as a recipe for generating an infinite number of production rules. Imagine a copy of the following rules for D = 1, 2, 3, .... D keeps track of the nesting depth. ==>