Chez Scheme是一种有着众多的语言和编程环境扩展的“Revised6 Report on Scheme [27] (R6RS)”实现。
本书详细描述了这些扩展。 亦包含了标准的和Chez Scheme的形式与过程的简明摘要,给出了每个形式和每个过程所接受的参数的数量和类型的语法。 标准Scheme特性的详细内容可参考The Scheme Programming Language, 4th Edition (TSPL4) [11]或者the Revised6 Report on Scheme. The Scheme Programming Language, 4th Edition也包含了一个关于Scheme语言的广泛的介绍和众多长长短短的例子。
本文档大部分内容也适用于Petite Chez Scheme(与完整版Chez Scheme系统完全兼容,但其使用了一个高速解释器替代了增量本地编译器)。 为Chez Scheme写的程序只要不调用编译器的话,那么无需修改就能在Petite Chez Scheme中运行, 事实上,Petite Chez Scheme编译自与Chez Scheme相同的代码,仅仅除去了与编译器相关的代码。 该区别造成的影响的详细讨论参见Section 2.8.
本章的其余部分将介绍Chez Scheme对Scheme语法的拓展(Section 1.1),本书中使用的符号约定(Section 1.2),用于系统自定义的参数用法(Section 1.3),与如何查找与Chez Scheme相关的更多信息(Section 1.4).
Chapter 2 描述了如何使用Chez Scheme来进行程序开发、脚本、应用交付,以及如何使编译器生成最高效的代码。
Chapter 3 描述了调试和对象检查功能(//object inspection facilities)。
Chapter 4 陈述了与不同处理器或其他语言代码交互的facilities。
Chapter 5 描述了绑定表格。
Chapter 6 陈述了控制结构。
Chapter 7 陈述了对非数字对象的操作。
Chapter 8 陈述了各种对数字的操作,包括各种数字操作、高效的特定类型操作。
Chapter 9 描述了输入/输出操作和允许定义具有任意输入/输出语义的通用端口。
Chapter 10 讨论了如何将R6RS库和顶层程序加载到Chez Scheme以及控制和跟踪加载过程的各种功能。
Chapter 11 描述了语法扩展和模块。
Chapter 12 描述了系统操作,例如与操作系统交互和定制Chez Scheme的用户界面的操作。
Chapter 13 描述了如何调用和控制存储管理系统并记录监护人和弱对。(//documents guardians and weak pairs.)
Chapter 14 描述了Chez Scheme的表达式编辑器以及如何对其定制。
Chapter 15 陈述了包括Chez Scheme的本地线程系统接口的过程和语法形式。
Chapter 16 描述了各种兼容性功能。
本书的后面包含了参考书目,语法形式摘要,和索引。 在语法形式摘要出现的页码以及在索引中出现的斜体页码指明了语法形式和过程被正式定义在文本中的位置 语法形式摘要和索引包含了来自TSPL4的条目,故而它们完整覆盖了Chez Scheme的功能。 TSPL4的条目以一个“t”字母为页码前缀作为标记。
本书和TSPL4的在线版本和勘误表可参见www.scheme.com.
致谢: Michael Adams, Mike Ashley, Carl Bruggeman, Bob Burger, Sam Daniel, George Davidson, Aziz Ghuloum, Bob Hieb, Andy Keep, and Oscar Waddell have contributed substantially to the development of Chez Scheme. Chez Scheme's expression editor is based on a command-line editor for Scheme developed from 1989 through 1994 by C. David Boyer. File compression is performed with the use of the zlib compression library developed by Jean-loup Gailly and Mark Adler. Implementations of the list and vector sorting routines are based on Olin Shiver's opportunistic merge-sort algorithm and implementation. Michael Lenaghan provided a number of corrections for earlier drafts of this book. Many of the features documented in this book were suggested by current Chez Scheme users, and numerous comments from users have also led to improvements in the text. Additional suggestions for improvements to Chez Scheme and to this book are welcome.
Chez Scheme同时在对象(数据(//datum))层面和语法形式层面拓展了Scheme的语法。 在对象层面,Chez Scheme支持包含非标准字符、以浮点数和科学计数法表达的非十进制数字、显式长度的向量、共享和环状结构、records,boxes等等的符号的额外表示法。 这些扩展将在下面描述。 语法形式层面的拓展的描述将贯穿本书,并且将总结在本书的后面的语法形式摘要中。
Chez Scheme以多种方式拓展了标识符的语法。 首先,组建标识符名字的字符序列可以以数字、句点、加号、减号为开头,只要整体不被解析为一个数字。 例如,0abc, +++, and ..在Chez Scheme中都是有效的标识符。 第二点,单字符序列{和}也是标识符。 第三点,包含任意字符的标识符将可以使用\或|来使用转义字符的方式打印。 \用来转义单个字符(除了‘x’,因为\x用于标记一个十六进制标量值的开始), 而|用来转义从|开始到|结束的中键的字符串组。 例如\||\|是一个有着两个字符的标识符,其由一个字符|后跟一个字符\组成,而|hit me!|则是一个包含着一个空格字符的标识符。
此外,gensyms(page 7.9)能写成以#{和}括起来的包含漂亮和独一无二两个名字的形式,例如#{g1426 e5g1c94g642dssw-a}。 它们也可以只用漂亮名字以#:作为前缀来书写,例如#:g1426。
任意2到36的基数(译注,二进制基数是2、十进制基数是10、十六进制基数是16)可以使用前缀#nr,其中的n表示基数。 大小写并不重要,所以写成#nR也能用。 从10到35的数字能用大/小写的英文字母表示,就像十六进制那么用一样。 例如#36rZZ表示35 × 36 + 35,也即1295。
Chez Scheme也允许非十进制数字以浮点数或科学计数法的方式打印 例如,#o1.4等于1.5,而#b1e10等于4.0。 数字写在指数的前面,所以#x1e20等同于四位十六进制数7712.
除了标准命名的字符 #\alarm, #\backspace, #\delete, #\esc, #\linefeed, #\newline, #\page, #\return, #\space, 和 #\tab, Chez Scheme也能识别 #\bel, #\ls, #\nel, #\nul, #\rubout, 和 #\vt (or #\vtab)。 标量值小于256的字符也能以#\为前缀后跟3个八进制数的八进制方式书写。 例如,#\000等同于#\nul。
Chez Scheme的fxvectors,或者换种说法fixnum vectors,能写成类似向量(vectors)但却以#vfx(为前缀而不是#(的形式。 向量(Vectors),bytevectors以及fxvectors都可以写成以一个明确的长度为前缀,并且当明确的长度被指定,重复的尾部元素可以被简写为一个。 例如,#(a b c)能写成#3(a b c),而一个包含100个全零的向量可以简写为#100(0)。
Chez Scheme的box以#&为前缀,例如#&17是一个包含整型数17的box。
Records能写成#[type-name field ...]这样的语法形式,其中符号type-name是record的类型名、field ...则表示record字段的内容。
共享和循环结构可写成使用图标记和相关前缀#n=和#n#的形式。 #n=用于标记输入端的项, 而#n#用于标记被标注为n的项。 例如,'(#1=(a) . #1#)是一个car和cdr包含着同一list的pair,而#0=(a . #0#)是一个循环list,也就是说,它的cdr是它本身。
一个$primitive form (详见page 336)可能会以quote form同样的方式,即使用#%为前缀,作为缩写。 例如, #%car等同于($primitive car), #2%car等同于($primitive 2 car), #3%car等同于($primitive 3 car),
Chez Scheme的end-of-file对象写作#!eof。 如果end-of-file对象出现在任何载入了的文件的数据外部(//If the end-of-file object appears outside of any datum within a file being loaded),load将把它视为一个真正的文件结尾并停止在这个位置的载入。 在一个文件的中间插入#!eof可以方便地跟踪load-time错误。
weak pairs中的Broken pointers(详见page 381)被表示为broken weak pointer对象,写作#!bwp。
除了标准的分隔符(空格,左右小括号,左右中括号,双引号,分号,以及#), Chez Scheme也将以下作为分隔符:左右大括号,单引号,反引号,逗号。
The Chez Scheme lexical extensions described above are disabled in an input stream after an #!r6rs comment directive has been seen, unless a #!chezscheme comment directive has been seen since. Each library loaded implicitly via import and each RNRS top-level program loaded via the --program command-line option, the scheme-script command, or the load-program procedure is treated as if it begins implicitly with an #!r6rs comment directive.
The case of symbol and character names is normally significant, as required by the Revised6 Report. Names are folded, as if by string-foldcase, following a #!fold-case comment directive in the same input stream unless a #!no-fold-case has been seen since. Names are also folded if neither directive has been seen and the parameter case-sensitive has been set to #f.
The printer invoked by write, put-datum, pretty-print, and the format ~s option always prints standard Revised6 Report objects using the standard syntax. For example, it prints symbols whose names require escaping using hex scalar value escapes, not one of the other mechanisms supported by the reader. Similarly, it does not print the explicit length or suppress duplicate trailing elements unless the parameter print-vector-length is set to true.
This book follows essentially the same notational conventions as The Scheme Programming Language, 4th Edition. These conventions are repeated below, with notes specific to Chez Scheme.
When the value produced by a procedure or syntactic form is said to be unspecified, the form or procedure may return any number of values, each of which may be any Scheme object. Chez Scheme usually returns a single, unique void object (see void) whenever the result is unspecified; avoid counting on this behavior, however, especially if your program may be ported to another Scheme implementation. Printing of the void object is suppressed by Chez Scheme's waiter (read-evaluate-print loop).
This book uses the words "must" and "should" to describe program requirements, such as the requirement to provide an index that is less than the length of the vector in a call to vector-ref. If the word "must" is used, it means that the requirement is enforced by the implementation, i.e., an exception is raised, usually with condition type &assertion. If the word "should" is used, an exception may or may not be raised, and if not, the behavior of the program is undefined. The phrase "syntax violation" is used to describe a situation in which a program is malformed. Syntax violations are detected prior to program execution. When a syntax violation is detected, an exception of type &syntax is raised and the program is not executed.
Scheme objects are displayed in a typewriter typeface just as they are to be typed at the keyboard. This includes identifiers, constant objects, parenthesized Scheme expressions, and whole programs. An italic typeface is used to set off syntax variables in the descriptions of syntactic forms and arguments in the descriptions of procedures. Italics are also used to set off technical terms the first time they appear. The first letter of an identifier that is not ordinarily capitalized is not capitalized when it appears at the beginning of a sentence. The same is true for syntax variables written in italics.
In the description of a syntactic form or procedure, a pattern shows the syntactic form or the application of the procedure. The syntax keyword or procedure name is given in typewriter font, as are parentheses. The remaining pieces of the syntax or arguments are shown in italics, using names that imply the types of the expressions or arguments expected by the syntactic form or procedure. Ellipses are used to specify zero or more occurrences of a subexpression or argument.
All Chez Scheme system customization is done via parameters. A parameter is a procedure that encapsulates a hidden state variable. When invoked without arguments, a parameter returns the value of the encapsulated variable. When invoked with one argument, the parameter changes the value of the variable to the value of its argument. A parameter may raise an exception if its argument is not appropriate, or it may filter the argument in some way.
New parameters may be created and used by programs running in Chez Scheme. Parameters are used rather than global variables for program customization for two reasons: First, unintentional redefinition of a customization variable can cause unexpected problems, whereas unintentional redefinition of a parameter simply makes the parameter inaccessible. For example, a program that defines *print-level* for its own purposes in early releases of Chez Scheme would have unexpected effects on the printing of Scheme objects, whereas a program that defines print-level for its own purposes simply loses the ability to alter the printer's behavior. Of course, a program that invokes print-level by accident can still affect the system in unintended ways, but such an occurrence is less likely, and can only happen in an incorrect program.
Second, invalid values for parameters can be detected and rejected immediately when the "assignment" is made, rather than at the point where the first use occurs, when it is too late to recover and reinstate the old value. For example, an assignment of *print-level* to -1 would not have been caught until the first call to write or pretty-print, whereas an attempted assignment of -1 to the parameter print-level, i.e., (print-level -1), is flagged as an error immediately, before the change is actually made.
Built-in system parameters are described in different sections throughout this book and are listed along with other syntactic forms and procedures in the Summary of Forms in the back of this book. Parameters marked "thread parameters" have per-thread values in threaded versions of Chez Scheme, while the values of parameters marked "global parameters" are shared by all threads. Nonthreaded versions of Chez Scheme do not distinguish between thread and global parameters. See Sections 12.13 and 15.6 for more information on creating and manipulating parameters.
The articles and technical reports listed below document various features of Chez Scheme and its implementation:
Links to abstracts and electronic versions of these publications are available at the url http://www.cs.indiana.edu/chezscheme/pubs/.
Chez Scheme Version 9 User's Guide
Copyright © 2016 Cisco Systems, Inc.
Licensed under the Apache License Version 2.0
(full copyright notice.).
Revised June 2016 for Chez Scheme Version 9.4
about this book