|
PLT MzScheme: Language Manual
|
(left-datum ···1 . first-datum . right-datum ···1) |
datum
between the dots,
followed by the remaining datum
s in order:
(first-datum left-datum ···1 right-datum ···1) |
(1 . < . 2)
produces
#t
, and (1 2 . + . 3 4 5)
produces 15
.
When the read-accept-dot
parameter is set to
#f
, then a delimited dot (``.
'') is disallowed in
input. When the read-accept-quasiquote
parameter is set to
#f
, then a backquote or comma is disallowed in input. These
modes simplify Scheme's input model for students.
MzScheme's identifier and symbol syntax is considerably more liberal than the syntax specified by R5RS. When input is scanned for tokens, the following characters delimit an identifier:
" , ' ` ( ) [ ] { } space tab return newline page vtab
In addition, an identifier cannot start with a hash mark
(``#
'') unless the hash mark is immediately followed by a
percent sign (``%
''). The only other special characters are
backslash (``\'') or quoting vertical bars (``|'');
any other character is used as part of an identifier.
Symbols containing special characters (including delimiters) are expressed using an escaping backslash (``\'') or quoting vertical bars (``|''):
A backslash preceding any character includes that character in the symbol literally; double backslashes produce a single backslash in the symbol.
Characters between a pair of vertical bars are included in the symbol literally. Quoting bars can be used for any part of a symbol, or the whole symbol can be quoted. Backslashes and quoting bars can be mixed within a symbol, but a backslash is not a special character within a pair of quoting bars.
Characters quoted with a backslash or a vertical bar always preserve their case, even when identifiers are read case-insensitively (the default).
An input token constructed in this way is an identifier when it is not a numerical constant (following the extended number syntax described in section 3.3). A token containing a backslash or vertical bars is never treated as a numerical constant.
Examples:
(
produces the same symbol as
quote
a\(b)(
.string->symbol
"a(b")
(
produces the same
symbol as quote
A\B)(
when identifiers are read
without case-sensitivity.string->symbol
"aB")
(
, quote
a\ b)(
, and
quote
|a b|)(
all produce the same symbol as
quote
a| |b)(
.string->symbol
"a b")
(
is the same as quote
|a||b|)(quote ab)
.
(
is the number 10, but quote
10)(
produces the same symbol as quote
|10|)(
.string->symbol
"10")
Whether a vertical bar is used as a special or normal symbol character
is controlled with the read-accept-bar-quote
boolean
parameter (see section 7.4.1.3). Vertical bar quotes
are enabled by default. Quoting backslashes cannot be disabled.
By default, symbols are read case-insensitively (i.e., uppercase characters are downcased). Case sensitivity for reading can be controlled in three ways:
Quoting part of a symbol with an escaping backslash (``\'') or quoting vertical bar (``|'') always preserves the case of the quoted portion, as described above.
The sequence #cs
can
be used as a prefix for any expression to make reading symbols
within the expression case-sensitive. A #ci
prefix
similarly makes reading symbols in an expression case-insensitive.
Whitespace can appear between a #cs
or #ci
prefix and its expression, and prefixes can be nested. Backslash and
vertical-bar quotes override a #ci
prefix.
When the read-case-sensitive
parameter (see
section 7.4.1.3) is set to #t
, then case is
preserved when reading symbols. The default is #f
, and it
is set to #f
while loading a module (see
section 5.8). A #cs
or #ci
prefix
overrides the parameter setting, as does backslash or vertical-bar
quoting.
Case conversions are not sensitive to the current locale (see section 7.4.1.11).
S-expressions with shared structure are expressed using
#
and n
=#
, where n
#n
is
a decimal integer. See section 14.5.
Expressions of the form #%
are symbols, where
x
x
can be a symbol or a number.
Expressions beginning with #~
are interpreted as
compiled MzScheme code. See section 14.6.
Multi-line comments are started with #|
and terminated
with |#
. Comments of this form can be nested arbitrarily.
If the first line of a
ed file begins with
load
#!
, it is ignored by the default load handler. If an ignored
line ends with a backslash (``\''), then the next line is
also ignored. (The #!
convention is for shell scripts; see
Chapter 18 for details.)
Reading from a custom port can produce arbitrary values generated by
the port; see section 11.1.6 for details. If the port generates
a non-character value in a position where a character is required
(e.g., within a string), the exn:read:non-char
exception is raised.
MzScheme's printer obeys the following non-standard rules:
A vector can be printed by
and write
using
the shorthand described in section 14.3, where the vector's
length is printed between the leading print
#
and the opening
parenthesis and repeated tail elements are omitted. For example,
#(1 2 2 2)
is printed as #4(1 2)
. The
procedure does not output vectors using this shorthand. Shorthand
vector printing is controlled with the display
print-vector-length
boolean parameter (see section 7.4.1.4). Shorthand
vector printing is enabled by default.
Boxes (see section 3.9) can be printed with the #\&
notation (see section 14.3). When box printing is disabled, all
boxes are printed as #<box>
. Box printing is controlled with
the print-box
boolean parameter (see
section 7.4.1.4). Box printing is enabled by default.
Structures (see Chapter 4) can be printed using
vector notation. In the vector, the first item is a symbol of the
form struct:
-- where s
s
is the name of the
structure -- and the remaining elements are the elements of the
structure, but the vector exposes only as much information about the
structure as the current inspector can access (see
section 4.6). When structure printing is disabled, or when no
part of the structure is accessible to the current inspector, a
structure is printed as #<struct:
. Structure printing is
controlled with the s
>print-struct
boolean parameter (see
section 7.4.1.4). Structure printing is disabled by default.
Symbols containing spaces or special characters
using escaping backslashes and quoting vertical bars. When the
write
read-case-sensitive
parameter is set to #f
, then
symbols containing uppercase characters also use escaping backslashes
or quoting vertical bars. In addition, symbols are quoted with
vertical bars or a leading backslash when they would otherwise print
the same as a numerical constant. If the value of the
read-accept-bar-quote
boolean parameter is #f
(see section 7.4.1.3), then backslashes are always
used to escape special characters instead of quoting them with
vertical bars, and a vertical bar is not treated as a special
character. See section 14.3 for more information about symbol
parsing. Symbols
without escaping or quoting special
characters.display
Characters with the special names described in section 14.3
using the same name. (Some characters have multiple
names; the write
#\newline
and #\nul
names are used
instead of #\linefeed
and #\null
). Other
``printable'' characters
as write
#\
followed by
the single-byte character value, and ``unprintable'' characters are
written in octal notation; unprintable-character detection depends on
locale sensitivity (see section 7.4.1.11), and when the
current locale is disabled, a character whose
value is greater than 127 is always treated as unprintable. All
characters char->integer
as the single-byte character value.display
Strings containing ``unprintable'' characters (see above)
using the escape sequences described in
section 14.3. All strings write
as their literal
character sequences.display
S-expressions with shared structure can be printed using
#
and n
=#
, where n
#n
is a decimal
integer. See section 14.5.
MzScheme can read and print graphs, S-expressions with
shared structure (e.g., a cycle). Graphs are described by tagging
the shared structure once with #
(using some decimal
integer n
=n
with no more than eight digits) and then referencing
it later with #
(using the same number n
#n
). For
example, the following S-expression describes the infinite list of
ones:
#0=(1 . #0#)
If this graph is entered into MzScheme's read
-eval
-print
loop, MzScheme's compiler
will loop forever, trying to compile an infinite expression. In
contrast, the following expression defines ones
to the
infinite list of ones, using quote
to hide the infinite list
from the compiler:
(define ones (quote #0=(1 . #0#)))
A tagged structure can be referenced multiple
times. Here, v
is defined to be a vector containing the same
cell in all three slots:
cons
(define v #(#1=(cons
1 2) #1# #1#))
A tag #
must appear to the left of all references
n
=#
, and all references must appear in the same top-level
S-expression as the tag. By default, MzScheme's printer will display
a value without showing the shared structure:
n
#
#((1 . 2) (1 . 2) (1 . 2))
Graph reading and printing are controlled with the
read-accept-graph
and print-graph
boolean
parameters (see section 7.4.1.4). Graph reading is enabled by
default, and graph printing is disabled by default. However, when the
printer encounters an graph containing a cycle, graph
printing is automatically enabled, temporarily. (For this reason, the
display
, write
, and print
procedures
require memory proportional to the depth of the value being printed.)
When graph reading is disabled and a graph is provided as input, the
exn:read
exception is raised.
If the n
in a #
form or a n
=#
form
contains more than eight digits, the n
#exn:read
exception is raised. If a
#
form is not preceded by a n
##
form
using the same n
=n
, the exn:read
exception is raised. If two
#
forms are in the same expression for the same
n
=n
, the exn:read
exception is raised.
Normally, compilation happens automatically: when an S-expression is
evaluated, it is first compiled and then the compiled code is
executed. However, MzScheme can also write and read compiled
code. MzScheme can read compiled code much faster than reading
S-expression code and compiling it, so compilation can be used to
speed up program loading. The MzLib procedure
(see Chapter 9
in PLT MzLib: Libraries Manual) is sufficient for most compilation
purposes.
compile-file
(compile
expr
)
compiles expr
, where expr
is any
S-expression that can be passed to
. The result is a
compiled expression Scheme value. This value can be passed to
eval
to evaluate the compiled expression.eval
(compiled-expression?
v
)
returns #t
if v
is
a compiled expression, #f
otherwise.
When a compiled expression is written to an output port, the written
form starts with #~
. These expressions are essentially
assembly code for the MzScheme interpreter, and reading such an
expression produces a compiled expression.
When a compiled expression contains syntax object constants, the
#~
form of the expression drops location information and
properties for the syntax objects (see section 12.2 and
section 12.6.2).
Never ask MzScheme to evaluate an expression starting with #~
unless
generated the expression. To keep users from
accidentally specifying bad instructions, compile
will not
accept expressions beginning with read
#~
unless it is
specifically enabled through the read-accept-compiled
boolean parameter (see section 7.4.1.3). When the
default load handler is used to load a file, compiled expression
reading is automatically (temporarily) enabled as each expression is
read.
A compiled code object may contain uninterned symbols (see
section 3.6) that were created by
,
gensym
, and
string->uninterned-symbol
generate-temporaries
. When the compiled object is read via
#~
, each uninterned symbol in the original expression is
mapped to a new uninterned symbol, where multiple instances of a
single symbol are consistently mapped to the same new symbol. The
original and new symbols have the same printed representation.
Special problems arise when an uninterned symbol is used to construct an identifier for a module's exported variable. Since a module and its importers are typically compiled and written as separate expressions, different uninterned symbols are generated for identifiers when the different modules are loaded. MzScheme corrects for the problem by recording identifier-position pairs for each import, and then resolving imports at load time by checking the printed representation of a name at the expected position in the exporting module. (In principle, the position is sufficient, but MzScheme checks the printed form of the name to help avoid confusion due to mis-ordered compilation sequences.)
A dynamically-linked extension library is loaded into MzScheme with
(load-extension
file-path
)
. The separate document
Inside PLT MzScheme contains information about writing MzScheme
extensions. An extension can only be loaded once during a MzScheme
session, although the extension-writer can provide functionality to
handle extra calls to
for a single extension.load-extension
As with
, the current load
directory (the
value of the load-relative
current-load-relative-directory
parameter;
see section 7.4.1.6) is set while the
extension is loaded. The load-relative-extension
procedure
is like
, but it loads an extension with a
pathname that is relative to the current load-extension
directory instead of the current directory.load-relative
The
procedure actually just dispatches to the
current load extension handler (see
section 7.4.1.6). The result of calling
load-extension
is determined by the extension. If the extension
cannot be loaded, the load-extension
exn:i/o:filesystem
exception is raised. The detail
field of the exception is 'wrong-version
if the load
fails because the extension has the wrong version.
An image is a memory dump from a running MzScheme program that can be later restored (one or more times) to continue running the program from the point of the dump. Images are only supported for statically-linked Unix versions of MzScheme (and MrEd). There are a few special restrictions on images:
All files and TCP connections must be closed when an image is created.
No dynamic extensions can be loaded before an image is created.
No operating system subprocesses can be active when an image is created.
(write-image-to-file
file-path
[cont-proc
])
copies the state of the entire MzScheme process35 to file-path
, replacing file-path
if it already exists. If images are not supported, the
exn:misc:unsupported
exception is raised. If cont-proc
is #f
,
then the MzScheme or MrEd process exits immediately after creating
the image. Otherwise, cont-proc
must be a procedure of no
arguments, and the return value(s) of the call to
is write-image-to-file
(cont-proc)
. The default
value for cont-proc
is
.void
(read-image-from-file
file-path arg-vector
)
restores the image
saved to file-path
. Once the image is restored, execution of
the original program continues with the return from
; the return value in the restored program
is the a vector of strings write-image-to-file
arg-vector
. A successful call to
never returns because the restored program
is overlayed over the current program. The vector read-image-from-file
arg-vector
must contain no more than 20 strings, and the total length of the
strings must be no more than 2048 characters.
If an error is encountered while reading or writing an image, the
exn:i/o:filesystem
exception is raised or exn:misc
exception is raised. Certain errors
during
are unrecoverable; in case of such
errors, MzScheme prints an error message and exits immediately.read-image-from-file
An image can also be restored by starting the stand-alone version of
MzScheme or MrEd with the --restore flag followed by the
image filename. The return value from
in
the restored program is a vector of strings that are the extra
arguments provided on the command line after the image filename (if
any).write-image-to-file
32 The
procedure actually calls the
current evaluation handler (see section 7.4.1.5) with
eval
e
to evaluate the expression.
33 The
procedure actually just sets the current load
directory and calls the current load handler (see
section 7.4.1.6) with load-relative
file-path
to load the file. The
description of
here is actually a description of the
default load handler.load
34 The
procedure actually just calls the current
load/use-compiled handler (see
section 7.4.1.6). The default handler, in turn,
calls the load or load-extension handler, depending on the type of
file that is loaded.load/use-compiled
35 The set of environment variables is not saved. When an image is restored, the environment variables of the restoring program are transferred into the restored program.