在本页中:
1.1 预定义变量
empty
true
false
1.2 模板变量
..
...
....
.....
......
1.3 语法
define
lambda
quote
define-struct
cond
else
if
and
or
check-expect
check-random
check-satisfied
check-within
check-error
check-member-of
check-range
require
1.4 预定义函数
1.5 数值:整数、有理数、实数、复数、精确数、非精确数
*
+
-
/
<
<=
=
>
>=
abs
acos
add1
angle
asin
atan
ceiling
complex?
conjugate
cos
cosh
current-seconds
denominator
e
even?
exact->inexact
exact?
exp
expt
floor
gcd
imag-part
inexact->exact
inexact?
integer->char
integer-sqrt
integer?
lcm
log
magnitude
make-polar
make-rectangular
max
min
modulo
negative?
number->string
number?
numerator
odd?
pi
positive?
quotient
random
rational?
real-part
real?
remainder
round
sgn
sin
sinh
sqr
sqrt
sub1
tan
zero?
1.6 布尔值
boolean->string
boolean=?
boolean?
false?
not
1.7 符号
symbol->string
symbol=?
symbol?
1.8 链表
append
assoc
assq
caaar
caadr
caar
cadar
cadddr
caddr
cadr
car
cdaar
cdadr
cdar
cddar
cdddr
cddr
cdr
cons
cons?
eighth
empty?
fifth
first
fourth
length
list
list*
list-ref
list?
make-list
member
member?
memq
memq?
memv
null
null?
range
remove
remove-all
rest
reverse
second
seventh
sixth
third
1.9 Posn
make-posn
posn-x
posn-y
posn?
1.10 字符
char->integer
char-alphabetic?
char-ci<=?
char-ci<?
char-ci=?
char-ci>=?
char-ci>?
char-downcase
char-lower-case?
char-numeric?
char-upcase
char-upper-case?
char-whitespace?
char<=?
char<?
char=?
char>=?
char>?
char?
1.11 字符串
explode
format
implode
int->string
list->string
make-string
replicate
string
string->int
string->list
string->number
string->symbol
string-alphabetic?
string-append
string-ci<=?
string-ci<?
string-ci=?
string-ci>=?
string-ci>?
string-contains-ci?
string-contains?
string-copy
string-downcase
string-ith
string-length
string-lower-case?
string-numeric?
string-ref
string-upcase
string-upper-case?
string-whitespace?
string<=?
string<?
string=?
string>=?
string>?
string?
substring
1.12 图像
image=?
image?
1.13 杂项
=~
eof
eof-object?
eq?
equal?
equal~?
eqv?
error
exit
identity
struct?

1 初级

语法符号使用符号X ...(粗体的点)表示X可能出现任意多次(零次,一次或多次)。此外,语法还将...定义为可以在模板中使用的标识符。

关于初级语言的解释,请参阅《程序设计方法(第二版)》的独立章节1

  program = def-or-expr ...
     
  def-or-expr = definition
  | expr
  | test-case
  | library-require
     
  definition = (define (name variable variable ...) expr)
  | (define name expr)
  | (define name (lambda (variable variable ...) expr))
  | (define-struct name (name ...))
     
  expr = (name expr expr ...)
  | (cond [expr expr] ... [expr expr])
  | (cond [expr expr] ... [else expr])
  | (if expr expr expr)
  | (and expr expr expr ...)
  | (or expr expr expr ...)
  | name
  | name
  | ()
  | number
  | boolean
  | string
  | character
     
  test-case = (check-expect expr expr)
  | (check-random expr expr)
  | (check-within expr expr expr)
  | (check-member-of expr expr ...)
  | (check-range expr expr expr)
  | (check-satisfied expr name)
  | (check-error expr expr)
  | (check-error expr)
     
  library-require = (require string)
  | (require (lib string string ...))
  | (require (planet string package))
     
  package = (string string number number)

namevariable是不包含空格或下列字符的字符串:
   " , ' ` ( ) [ ] { } | ; #
number是类似1233/25.5的数。
boolean#true#false之一。常量#true的其他拼写方式是:#ttrue#T。同理,#ffalse#F都被认为是#false
symbol是引号字符后跟name。符号是一种值,就和42'()#false一样。
string是由一对"包围的字符序列。与符号不同,字符串可以被分割成字符,或者用各种函数操作。例如,"abcdef""This is a string""This is a string with \" inside"都是字符串。
character#\开始,并包含该字符的名称。例如,#\a#\b#\space都是字符。
函数调用中,紧跟开括号出现的函数可以是由definedefine-struct定义的任意函数,或者是任何的预定义函数

1.1 预定义变量

empty : empty?

空表。

true : boolean?

#true值。
#false值。

1.2 模板变量

语法

..

表明定义是模版的占位符。

语法

...

表明定义是模版的占位符。

语法

....

表明定义是模版的占位符。

语法

.....

表明定义是模版的占位符。

语法

......

表明定义是模版的占位符。

1.3 语法

语法

(define (name variable variable ...) expression)

定义名为name的函数。expression为函数体。调用函数时,实际参数值会替换函数体中的variable们。新表达式的值就是函数的返回值。

函数名不能与其他函数或变量相同。

语法

(define name expression)

定义名为name的变量,其值为expression的值。变量名不能与其他函数或变量相同,而且name自身也不能出现在expression中。

语法

(define name (lambda (variable variable ...) expression))

另一种定义函数的方法。name是函数名,它不能与其他函数或变量相同。

除了这种语法之外,不能使用lambda

语法

name

语法

(quote name)

引用的name就是符号。符号是一种值,就和0'()一样。

语法

(define-struct structure-name (field-name ...))

定义名为structure-name的新结构体。结构体的字段名由field-name们给定。define-struct 会定义下列函数:

define-struct 引入的新函数名必须不同与其他函数或变量,否则define-struct 会报告错误。

语法

(name expression expression ...)

调用名为name的函数。函数调用的返回值是name函数体的值,其中每个函数的参数都被替换为对应expression的值。

名为name的函数必须在可以调用之前定义。参数expression的数量必须和函数所期望的参数数量一致。

语法

(cond [question-expression answer-expression] ...)

(cond [question-expression answer-expression]
      ...
      [else answer-expression])
根据条件选择子句。cond找出第一个计算为#truequestion-expression,然后计算对应的answer-expression

如果没有question-expression的计算结果为#true ,那么cond 的值是else 子句中的answer-expression。如果不存在else 子句,cond 报告错误。如果某个question-expression的值既不是#true 也不是#falsecond 也报告错误。

不能在cond之外使用else

语法

(if question-expression
    then-answer-expression
    else-answer-expression)
如果question-expression的值是#trueif 计算then-answer-expression的值。如果测试得到#falseif 计算else-answer-expression的值。

如果question-expression既不是#true 也不是#falseif 报告错误。

语法

(and expression expression expression ...)

如果所有expression都求值为#trueand 表达式求值为#true 。如果任何expression#falseand 表达式计算为#false (并且此表达式右侧的表达式不会被求值)。

如果任何一个表达式求值既不是#true 也不是#falseand 报告错误。

语法

(or expression expression expression ...)

只要一个expression求值为#trueor 表达式就求值为#true (并且此表达式右侧的表达式不会被求值)。如果所有expression都是#falseor 表达式求值为#false

如果任何一个表达式求值既不是#true 也不是#falseor 报告错误。

语法

(check-expect expression expected-expression)

检查第一个expression求得的值和expected-expression相同。

(check-expect (fahrenheit->celsius 212) 100)
(check-expect (fahrenheit->celsius -40) -40)
 
(define (fahrenheit->celsius f)
  (* 5/9 (- f 32)))
check-expect表达式必须被放在教学语言的顶层。它还可以出现在程序的任何地方,包括被测试函数定义之前。这样放置check-expect的话,程序员通过工作示例向未来的阅读者传达程序背后的意图,从而使阅读函数定义变得多余。

exprexpected-expr返回非精确数或函数是一种错误。对非精确数,简单比较它们原则上就是错误的。取而代之的做法,测试它们是否在某个很小的区间内;参见check-within。至于函数(参见中级及之后的语言),函数的比较是不可操作的。

语法

(check-random expression expected-expression)

检查第一个expression求得的值和expected-expression相同。

check-random为其两个部分提供相同的随机数生成器。如果两者以相同的顺序从相同的区间中取random数的话,它们会得到相同的随机数。

以下是check-random用途的简单示例:
(define WIDTH 100)
(define HEIGHT (* 2 WIDTH))
 
(define-struct player (name x y))
; Player(make-player String Nat Nat)
 
; String -> Player
 
(check-random (create-randomly-placed-player "David Van Horn")
              (make-player "David Van Horn" (random WIDTH) (random HEIGHT)))
 
(define (create-randomly-placed-player name)
  (make-player name (random WIDTH) (random HEIGHT)))
注意这里两个部分以同样的数值、同样的顺序调用random。如果两者调用random的区间不同,检查应该会失败:
; String -> Player
 
(check-random (create-randomly-placed-player "David Van Horn")
              (make-player "David Van Horn" (random WIDTH) (random HEIGHT)))
 
(define (create-randomly-placed-player name)
  (local ((define h (random HEIGHT))
          (define w (random WIDTH)))
    (make-player name w h)))

exprexpected-expr返回非精确数或函数是一种错误;详情参见check-expect的说明。

语法

(check-satisfied expression predicate)

检查第一个expression是否满足名为predicate的谓词(单参数函数)。回忆一下,“满足”的意思是“函数对输入值返回#true。”

以下是check-satisfied的简单示例:
> (check-satisfied 1 odd?)

The only test passed!

> (check-satisfied 1 even?)

Ran 1 check.

0 checks passed.

Actual value 1 does not satisfy "even?".

 At line 3 column 0

一般来说,check-satisfied使程序员可以使用已经定义好的函数来编写测试套件:
; [cons Number [List-of Number]] -> Boolean
; 测试htdp-sort的函数
 
(check-expect (sorted? (list 1 2 3)) #true)
(check-expect (sorted? (list 2 1 3)) #false)
 
(define (sorted? l)
  (cond
    [(empty? (rest l)) #true]
    [else (and (<= (first l) (second l)) (sorted? (rest l)))]))
 
; [List-of Number] -> [List-of Number]
; 创建输入数值表的排序版本
 
(check-satisfied (htdp-sort (list 1 2 0 3)) sorted?)
 
(define (htdp-sort l)
  (cond
    [(empty? l) l]
    [else (insert (first l) (htdp-sort (rest l)))]))
 
; Number [List-of Number] -> [List-of Number]
; x插入[l]中的合适位置上
; 假设l按降序排列
; 返回值也按降序排列
(define (insert x l)
  (cond
    [(empty? l) (list x)]
    [else (if (<= x (first l)) (cons x l) (cons (first l) (insert x (rest l))))]))

是的,htdp-sort 的返回值满足sorted?谓词:
> (check-satisfied (htdp-sort (list 1 2 0 3)) sorted?)

The only test passed!

语法

(check-within expression expected-expression delta)

检查expression表达式的值是否结构上等同于expected-expression表达式返回的值;前一个表达式中所有的数值都必须在后一个表达式对应数值的delta范围内。

(define-struct roots (x sqrt))
; RT is [List-of (make-roots Number Number)]
 
(define (roots-table xs)
  (map (lambda (a) (make-roots a (sqrt a))) xs))

鉴于嵌套数据中存在非精确数,check-within是正确的测试工具,如果delta足够大,测试就会通过:

例如:
> (check-within (roots-table (list 1.0 2.0 3.0))
                (list
                  (make-roots 1.0 1.0)
                  (make-roots 2  1.414)
                  (make-roots 3  1.713))
                0.1)

The only test passed!

反之,如果delta很小,测试就会失败:

例如:
> (check-within (roots-table (list 2.0))
                (list
                  (make-roots 2  1.414))
                1e-05)

Ran 1 check.

0 checks passed.

Actual value '((make-roots 2.0 1.4142135623730951)) is not within 1e-05 of expected value '((make-roots 2 1.414)).

 At line 5 column 0

expressionsexpected-expression返回函数是一种错误;详情参见check-expect的说明。

如果delta不是数值,check-within 报告错误。

语法

(check-error expression expected-error-message)

(check-error expression)
检查expression报告错误,并且如果有错误信息的话,它符合expected-error-message的值。

这是一个典型的初级语言中需要用到check-error的例子:
(define sample-table
  '(("matthias" 10)
    ("matthew"  20)
    ("robby"    -1)
    ("shriram"  18)))
 
; [List-of [list String Number]] String -> Number
; table中对应于s的数值
 
(define (lookup table s)
  (cond
    [(empty? table) (error (string-append s " not found"))]
    [else (if (string=? (first (first table)) s)
              (second (first table))
              (lookup (rest table)))]))

考虑以下两个例子:

例如:
> (check-expect (lookup sample-table "matthew") 20)

The only test passed!

例如:
> (check-error (lookup sample-table "kathi") "kathi not found")

The only test passed!

语法

(check-member-of expression expression expression ...)

检查第一个expression的值是后面某个expression表达式的值。

; [List-of X] -> X
; 从输入表l中随机选择一个元素
(define (pick-one l)
  (list-ref l (random (length l))))

例如:
> (check-member-of (pick-one '("a" "b" "c")) "a" "b" "c")

The only test passed!

任何expressions返回函数都是错误;详情参见check-expect的说明。

语法

(check-range expression low-expression high-expression)

检查第一个expression的值是位于low-expressionhigh-expression值(包含)之间的数值。

check-range形式最适合用来给返回非精确数的函数确定范围:
; [Real -> Real] Real -> Real
; fx的斜率是多少?
(define (differentiate f x)
  (local ((define epsilon 0.001)
          (define left (- x epsilon))
          (define right (+ x epsilon))
          (define slope
            (/ (- (f right) (f left))
               2 epsilon)))
    slope))
 
(check-range (differentiate sin 0) 0.99 1.0)

expressionlow-expressionhigh-expression返回函数或非精确数是一种错误;详情参见check-expect的说明。

语法

(require string)

使由string指定的module中的定义在当前module(即文件)中可用,其中string指向与相对于当前文件的文件。

为了避免不同平台上路径的问题,这个string受到好几种限制:/是目录之间的分隔符,.永远表示当前目录,..总是表示父目录,路径的元素只能包含az(大小写)、09-_.,并且该字符串不能为空,也不能在开头或结尾位置包含/

语法

(require module-name)

访问已安装库中的文件。这里的库名是个标识符,其约束条件与相对路径字符串相同(尽管不包含引号),此外它不能包含.

语法

(require (lib string string ...))

访问已安装库中的文件,使其定义在当前module(即当前文件)中可用。第一个string指定了库文件名,后续string指定文件所在的collection(以及子collection等)。每个string都受到和(require string)中一样的限制。

语法

(require (planet string (string string number number)))

语法

(require (planet id))

语法

(require (planet string))

访问在因特网上通过PLaneT服务器分发的库,使其中的定义在当前module(即当前文件)中可用。

planet requires的完整语法在Importing and Exporting: require and provide中给出,但找到语法示例的最佳位置位于PLaneT 服务器上特定package的描述中。

1.4 预定义函数

后续小节列出了编程语言中内置的函数。所有其他函数要么从教学包中导入,要么必须在程序中定义。

1.5 数值:整数、有理数、实数、复数、精确数、非精确数

函数

(* x y z ...)  number

  x : number
  y : number
  z : number
将所有数值相乘。
> (* 5 3)

15

> (* 5 3 2)

30

函数

(+ x y z ...)  number

  x : number
  y : number
  z : number
将所有数值相加。
> (+ 2/3 1/16)

35/48

> (+ 3 2 5 8)

18

函数

(- x y ...)  number

  x : number
  y : number
从第一个数值中减去第二个(和后续)数值;如果只有一个参数,则对数值取相反数。
> (- 5)

-5

> (- 5 3)

2

> (- 5 3 1)

1

函数

(/ x y z ...)  number

  x : number
  y : number
  z : number
第一个数值除以第二个(和后续)数值。
> (/ 12 2)

6

> (/ 12 2 3)

2

函数

(< x y z ...)  boolean?

  x : real
  y : real
  z : real
比较(实)数小于。
> (< 42 2/5)

#false

函数

(<= x y z ...)  boolean?

  x : real
  y : real
  z : real
比较(实)数小于等于。
> (<= 42 2/5)

#false

函数

(= x y z ...)  boolean?

  x : number
  y : number
  z : number
比较数值相等。
> (= 42 2/5)

#false

函数

(> x y z ...)  boolean?

  x : real
  y : real
  z : real
比较(实)数大于。
> (> 42 2/5)

#true

函数

(>= x y z ...)  boolean?

  x : real
  y : real
  z : real
比较(实)数大于等于。
> (>= 42 42)

#true

函数

(abs x)  real

  x : real
求实数的绝对值。
> (abs -12)

12

函数

(acos x)  number

  x : number
计算数的反余弦(余弦的倒数)。
> (acos 0)

#i1.5707963267948966

函数

(add1 x)  number

  x : number
将给定的数值加一。
> (add1 2)

3

函数

(angle x)  real

  x : number
从复数中提取幅角。
> (angle (make-polar 3 4))

#i-2.2831853071795867

函数

(asin x)  number

  x : number
计算数的反正弦(正弦的倒数)。
> (asin 0)

0

函数

(atan x)  number

  x : number
计算给定数的反正切值:
> (atan 0)

0

> (atan 0.5)

#i0.4636476090008061

它还有一个双参数版本,(atan x y)计算(atan (/ x y)),不过x和y的符号确定结果的象限,在边界情况下结果往往比单参数版本更精确:
> (atan 3 4)

#i0.6435011087932844

> (atan -2 -1)

#i-2.0344439357957027

函数

(ceiling x)  integer

  x : real
求大于某个实数的最接近整数(精确或不精确)。参见round
> (ceiling 12.3)

#i13.0

函数

(complex? x)  boolean?

  x : any/c
判断某个值是否是复数。
> (complex? 1-2i)

#true

函数

(conjugate x)  number

  x : number
翻转复数虚部的符号(共轭复数)。
> (conjugate 3+4i)

3-4i

> (conjugate -2-5i)

-2+5i

> (conjugate (make-polar 3 4))

#i-1.960930862590836+2.2704074859237844i

函数

(cos x)  number

  x : number
计算数的余弦值(弧度)。
> (cos pi)

#i-1.0

函数

(cosh x)  number

  x : number
计算数的双曲余弦。
> (cosh 10)

#i11013.232920103324

函数

(current-seconds)  integer

确定当前时间,由(自平台特定的开始日期起)已经过的秒数形式给出。
> (current-seconds)

1536540051

函数

(denominator x)  integer

  x : rational?
计算有理数的分母。
> (denominator 2/3)

3

e : real

欧拉数。
> e

#i2.718281828459045

函数

(even? x)  boolean?

  x : integer
判断某个整数(精确或不精确)是否为偶数。
> (even? 2)

#true

函数

(exact->inexact x)  number

  x : number
将精确数转换为不精确数。
> (exact->inexact 12)

#i12.0

函数

(exact? x)  boolean?

  x : number
判断某个数值是否精确。
> (exact? (sqrt 2))

#false

函数

(exp x)  number

  x : number
求e的指数。
> (exp -2)

#i0.1353352832366127

函数

(expt x y)  number

  x : number
  y : number
计算第一个数的第二个数次幂。
> (expt 16 1/2)

4

> (expt 3 -4)

1/81

函数

(floor x)  integer

  x : real
求小于某个实数的最接近整数(精确或不精确)。参见round
> (floor 12.3)

#i12.0

函数

(gcd x y ...)  integer

  x : integer
  y : integer
确定两个整数(精确或不精确)的最大公约数。
> (gcd 6 12 8)

2

函数

(imag-part x)  real

  x : number
从复数中提取虚部。
> (imag-part 3+4i)

4

函数

(inexact->exact x)  number

  x : number
将不精确数转换为近似的精确数。
> (inexact->exact 12.0)

12

函数

(inexact? x)  boolean?

  x : number
判断某个数值是否不精确。
> (inexact? 1-2i)

#false

函数

(integer->char x)  char

  x : exact-integer?
在ASCII表中查找输入精确整数所对应的字符(如果存在的话)。
> (integer->char 42)

#\*

函数

(integer-sqrt x)  complex

  x : integer
计算一个整数的整数或虚整数平方根。
> (integer-sqrt 11)

3

> (integer-sqrt -11)

0+3i

函数

(integer? x)  boolean?

  x : any/c
判断某个值是否为整数(精确或不精确)。
> (integer? (sqrt 2))

#false

函数

(lcm x y ...)  integer

  x : integer
  y : integer
确定两个整数(精确或不精确)的最小公倍数。
> (lcm 6 12 8)

24

函数

(log x)  number

  x : number
求一个数(以e为底)的对数。
> (log 12)

#i2.4849066497880004

函数

(magnitude x)  real

  x : number
求复数的绝对值。
> (magnitude (make-polar 3 4))

#i3.0

函数

(make-polar x y)  number

  x : real
  y : real
由绝对值和幅角创建复数。
> (make-polar 3 4)

#i-1.960930862590836-2.2704074859237844i

函数

(make-rectangular x y)  number

  x : real
  y : real
由实部和虚部创建复数。
> (make-rectangular 3 4)

3+4i

函数

(max x y ...)  real

  x : real
  y : real
最大值。
> (max 3 2 8 7 2 9 0)

9

函数

(min x y ...)  real

  x : real
  y : real
最小值。
> (min 3 2 8 7 2 9 0)

0

函数

(modulo x y)  integer

  x : integer
  y : integer
求第一个数字除以第二个数字的余数:
> (modulo 9 2)

1

> (modulo 3 -4)

-1

函数

(negative? x)  boolean?

  x : real
判断某个实数是否严格小于零。
> (negative? -2)

#true

函数

(number->string x)  string

  x : number
将数值转换为字符串。
> (number->string 42)

"42"

函数

(number? n)  boolean?

  n : any/c
确定某个值是否为数值:
> (number? "hello world")

#false

> (number? 42)

#true

函数

(numerator x)  integer

  x : rational?
计算有理数的分子。
> (numerator 2/3)

2

函数

(odd? x)  boolean?

  x : integer
判断某个整数(精确或不精确)是否为奇数。
> (odd? 2)

#false

pi : real

圆的周长与其直径的比率。
> pi

#i3.141592653589793

函数

(positive? x)  boolean?

  x : real
判断某个实数是否严格大于零。
> (positive? -2)

#false

函数

(quotient x y)  integer

  x : integer
  y : integer
将第一个整数(被除数)除以第二个整数(除数)来获得商商。
> (quotient 9 2)

4

> (quotient 3 4)

0

函数

(random x)  natural

  x : natural
生成小于输入精确自然数的随机自然数。
> (random 42)

34

函数

(rational? x)  boolean?

  x : any/c
判断某个值是否为有理数。
> (rational? 1)

#true

> (rational? -2.349)

#true

> (rational? #i1.23456789)

#true

> (rational? (sqrt -1))

#false

> (rational? pi)

#true

> (rational? e)

#true

> (rational? 1-2i)

#false

正如交互所表明的那样,教学语言将很多数值视为有理数。特别地,pi是一个有理数,因为它只是对数学中π的有限近似。将rational?理解为建议将这些数视为分数。

函数

(real-part x)  real

  x : number
从复数中提取实部。
> (real-part 3+4i)

3

函数

(real? x)  boolean?

  x : any/c
判断某个值是否是实数。
> (real? 1-2i)

#false

函数

(remainder x y)  integer

  x : integer
  y : integer
确定将第一个除以第二个整数(精确或不精确)的余数。
> (remainder 9 2)

1

> (remainder 3 4)

3

函数

(round x)  integer

  x : real
将实数舍入为整数(如果到两个数距离一样,舍入到偶数)。参见floorceiling
> (round 12.3)

#i12.0

函数

(sgn x)  (union 1 #i1.0 0 #i0.0 -1 #i-1.0)

  x : real
求实数的符号。
> (sgn -12)

-1

函数

(sin x)  number

  x : number
计算数的正弦值(弧度)。
> (sin pi)

#i1.2246467991473532e-16

函数

(sinh x)  number

  x : number
计算数的双曲正弦。
> (sinh 10)

#i11013.232874703393

函数

(sqr x)  number

  x : number
计算一个数的平方。
> (sqr 8)

64

函数

(sqrt x)  number

  x : number
计算一个数的平方根。
> (sqrt 9)

3

> (sqrt 2)

#i1.4142135623730951

函数

(sub1 x)  number

  x : number
将给定的数值减一。
> (sub1 2)

1

函数

(tan x)  number

  x : number
计算数的正切值(弧度)。
> (tan pi)

#i-1.2246467991473532e-16

函数

(zero? x)  boolean?

  x : number
判断某个数值是否为零。
> (zero? 2)

#false

1.6 布尔值

函数

(boolean->string x)  string

  x : boolean?
将布尔值转换为字符串。
> (boolean->string #false)

"#false"

> (boolean->string #true)

"#true"

函数

(boolean=? x y)  boolean?

  x : boolean?
  y : boolean?
判断两个布尔值是否相等。
> (boolean=? #true #false)

#false

函数

(boolean? x)  boolean?

  x : any/c
判断某个值是否为布尔值。
> (boolean? 42)

#false

> (boolean? #false)

#true

函数

(false? x)  boolean?

  x : any/c
判断值是否为false。
> (false? #false)

#true

函数

(not x)  boolean?

  x : boolean?
对布尔值取反。
> (not #false)

#true

1.7 符号

函数

(symbol->string x)  string

  x : symbol
将符号转换为字符串。
> (symbol->string 'c)

"c"

函数

(symbol=? x y)  boolean?

  x : symbol
  y : symbol
判断两个符号是否相等。
> (symbol=? 'a 'b)

#false

函数

(symbol? x)  boolean?

  x : any/c
判断某个值是否为符号。
> (symbol? 'a)

#true

1.8 链表

函数

(append x y z ...)  list?

  x : list?
  y : list?
  z : list?
连接多个表中的项创建单个链表。
> (append (cons 1 (cons 2 '())) (cons "a" (cons "b" empty)))

(list 1 2 "a" "b")

函数

(assoc x l)  (union (listof any) #false)

  x : any
  l : (listof any)
返回l中第一个firstequal?x的序对;如果不存在这样的序对就返回#false
> (assoc "hello" '(("world" 2) ("hello" 3) ("good" 0)))

(list "hello" 3)

函数

(assq x l)  (union #false cons?)

  x : any/c
  l : list?
判断某个项是否是表中某个序对的第一项。(使用eq?进行比较。)
> a

(list (list 'a 22) (list 'b 8) (list 'c 70))

> (assq 'b a)

(list 'b 8)

函数

(caaar x)  any/c

  x : list?
LISP式的选择函数:(car (car (car (car x)))).
> w

(list (list (list (list "bye") 3) #true) 42)

> (caaar w)

(list "bye")

函数

(caadr x)  any/c

  x : list?
LISP式的选择函数:(car (car (cdr x))).
> (caadr (cons 1 (cons (cons 'a '()) (cons (cons 'd '()) '()))))

'a

函数

(caar x)  any/c

  x : list?
LISP式的选择函数:(car (car x)).
> y

(list (list (list 1 2 3) #false "world"))

> (caar y)

(list 1 2 3)

函数

(cadar x)  any/c

  x : list?
LISP式的选择函数:(car (cdr (car x))).
> w

(list (list (list (list "bye") 3) #true) 42)

> (cadar w)

#true

函数

(cadddr x)  any/c

  x : list?
LISP式的选择函数:(car (cdr (cdr (cdr x)))).
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (cadddr v)

4

函数

(caddr x)  any/c

  x : list?
LISP式的选择函数:(car (cdr (cdr x))).
> x

(list 2 "hello" #true)

> (caddr x)

#true

函数

(cadr x)  any/c

  x : list?
LISP式的选择函数:(car (cdr x)).
> x

(list 2 "hello" #true)

> (cadr x)

"hello"

函数

(car x)  any/c

  x : cons?
提取非空表的第一项。
> x

(list 2 "hello" #true)

> (car x)

2

函数

(cdaar x)  any/c

  x : list?
LISP式的选择函数:(cdr (car (car x))).
> w

(list (list (list (list "bye") 3) #true) 42)

> (cdaar w)

(list 3)

函数

(cdadr x)  any/c

  x : list?
LISP式的选择函数:(cdr (car (cdr x))).
> (cdadr (list 1 (list 2 "a") 3))

(list "a")

函数

(cdar x)  list?

  x : list?
LISP式的选择函数:(cdr (car x)).
> y

(list (list (list 1 2 3) #false "world"))

> (cdar y)

(list #false "world")

函数

(cddar x)  any/c

  x : list?
LISP式的选择函数:(cdr (cdr (car x)))
> w

(list (list (list (list "bye") 3) #true) 42)

> (cddar w)

'()

函数

(cdddr x)  any/c

  x : list?
LISP式的选择函数:(cdr (cdr (cdr x))).
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (cdddr v)

(list 4 5 6 7 8 9 'A)

函数

(cddr x)  list?

  x : list?
LISP式的选择函数:(cdr (cdr x)).
> x

(list 2 "hello" #true)

> (cddr x)

(list #true)

函数

(cdr x)  any/c

  x : cons?
提取非空表的其余项。
> x

(list 2 "hello" #true)

> (cdr x)

(list "hello" #true)

函数

(cons x y)  list?

  x : any/x
  y : list?
构造链表。
> (cons 1 '())

(cons 1 '())

函数

(cons? x)  boolean?

  x : any/c
判断某个值是否为cons构造的表。
> (cons? (cons 1 '()))

#true

> (cons? 42)

#false

函数

(eighth x)  any/c

  x : list?
提取非空表的第八项。
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (eighth v)

8

函数

(empty? x)  boolean?

  x : any/c
判断某个值是否为空表。
> (empty? '())

#true

> (empty? 42)

#false

函数

(fifth x)  any/c

  x : list?
提取非空表的第五项。
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (fifth v)

5

函数

(first x)  any/c

  x : cons?
提取非空表的第一项。
> x

(list 2 "hello" #true)

> (first x)

2

函数

(fourth x)  any/c

  x : list?
提取非空表的第四项。
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (fourth v)

4

函数

(length l)  natural-number?

  l : list?
计算表中的项的数量。
> x

(list 2 "hello" #true)

> (length x)

3

函数

(list x ...)  list?

  x : any/c
用参数构造表。
> (list 1 2 3 4 5 6 7 8 9 0)

(cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 (cons 0 '()))))))))))

函数

(list* x ... l)  list?

  x : any/c
  l : list?
通过将多个项添加到表中构造链表。
> x

(list 2 "hello" #true)

> (list* 4 3 x)

(list 4 3 2 "hello" #true)

函数

(list-ref x i)  any/c

  x : list?
  i : natural?
提取表的索引项。
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (list-ref v 9)

'A

函数

(list? x)  boolean?

  x : any
检查给定值是否为链表。
> (list? 42)

#false

> (list? '())

#true

> (list? (cons 1 (cons 2 '())))

#true

函数

(make-list i x)  list?

  i : natural-number
  x : any/c
创建ix的链表.
> (make-list 3 "hello")

(cons "hello" (cons "hello" (cons "hello" '())))

函数

(member x l)  boolean?

  x : any/c
  l : list?
判断某个值是否在表中(使用equal?进行比较)。
> x

(list 2 "hello" #true)

> (member "hello" x)

#true

函数

(member? x l)  boolean?

  x : any/c
  l : list?
判断某个值是否在表中(使用equal?进行比较)。
> x

(list 2 "hello" #true)

> (member? "hello" x)

#true

函数

(memq x l)  boolean?

  x : any/c
  l : list?
判断某个值x是否在某个表l中,使用eq?比较xl中的项。
> x

(list 2 "hello" #true)

> (memq (list (list 1 2 3)) x)

#false

函数

(memq? x l)  boolean?

  x : any/c
  l : list?
判断某个值x是否在某个表l中,使用eq?比较xl中的项。
> x

(list 2 "hello" #true)

> (memq? (list (list 1 2 3)) x)

#false

函数

(memv x l)  (or/c #false list)

  x : any/c
  l : list?
判断某个值是否在表中,如果是的话,返回以x开头表的后半部分。(使用eqv?谓词比较值。)
> x

(list 2 "hello" #true)

> (memv (list (list 1 2 3)) x)

#false

null : list

空表的另一个名称
> null

'()

函数

(null? x)  boolean?

  x : any/c
判断某个值是否为空表。
> (null? '())

#true

> (null? 42)

#false

函数

(range start end step)  list?

  start : number
  end : number
  step : number
通过从start每步走过stepend构造表。
> (range 0 10 2)

(cons 0 (cons 2 (cons 4 (cons 6 (cons 8 '())))))

函数

(remove x l)  list?

  x : any/c
  l : list?
构造类似输入表的表,删除第一个匹配输入项的项(使用equal?进行比较)。
> x

(list 2 "hello" #true)

> (remove "hello" x)

(list 2 #true)

> hello-2

(list 2 "hello" #true "hello")

> (remove "hello" hello-2)

(list 2 #true "hello")

函数

(remove-all x l)  list?

  x : any/c
  l : list?
构造类似输入表的表,删除输入项的所有出现(使用equal?进行比较)。
> x

(list 2 "hello" #true)

> (remove-all "hello" x)

(list 2 #true)

> hello-2

(list 2 "hello" #true "hello")

> (remove-all "hello" hello-2)

(list 2 #true)

函数

(rest x)  any/c

  x : cons?
提取非空表的其余项。
> x

(list 2 "hello" #true)

> (rest x)

(list "hello" #true)

函数

(reverse l)  list

  l : list?
创建链表的反转版本。
> x

(list 2 "hello" #true)

> (reverse x)

(list #true "hello" 2)

函数

(second x)  any/c

  x : list?
提取非空表的第二项。
> x

(list 2 "hello" #true)

> (second x)

"hello"

函数

(seventh x)  any/c

  x : list?
提取非空表的第七项。
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (seventh v)

7

函数

(sixth x)  any/c

  x : list?
提取非空表的第六项。
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (sixth v)

6

函数

(third x)  any/c

  x : list?
提取非空表的第三项。
> x

(list 2 "hello" #true)

> (third x)

#true

1.9 Posn

函数

(make-posn x y)  posn

  x : any/c
  y : any/c
用任意两个值构造posn。
> (make-posn 3 3)

(make-posn 3 3)

> (make-posn "hello" #true)

(make-posn "hello" #true)

函数

(posn-x p)  any

  p : posn
提取posn的x分量。
> p

(make-posn 2 -3)

> (posn-x p)

2

函数

(posn-y p)  any

  p : posn
提取posn的y分量。
> p

(make-posn 2 -3)

> (posn-y p)

-3

函数

(posn? x)  boolean?

  x : any/c
判断输入是否是posn。
> q

(make-posn "bye" 2)

> (posn? q)

#true

> (posn? 42)

#false

1.10 字符

函数

(char->integer c)  integer

  c : char
查找ASCII表中输入字符所对应的数值(如果有的话)。
> (char->integer #\a)

97

> (char->integer #\z)

122

函数

(char-alphabetic? c)  boolean?

  c : char
判断字符是否表示字母。
> (char-alphabetic? #\Q)

#true

函数

(char-ci<=? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否以不区分大小写的方式按增加的顺序排列。
> (char-ci<=? #\b #\B)

#true

> (char<=? #\b #\B)

#false

函数

(char-ci<? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否以不区分大小写的方式按严格增加的顺序排列。
> (char-ci<? #\B #\c)

#true

> (char<? #\b #\B)

#false

函数

(char-ci=? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断两个字符是否以不区分大小写的方式相等。
> (char-ci=? #\b #\B)

#true

函数

(char-ci>=? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否以不区分大小写的方式按减少的顺序排列。
> (char-ci>=? #\b #\C)

#false

> (char>=? #\b #\C)

#true

函数

(char-ci>? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否以不区分大小写的方式按严格减少的顺序排列。
> (char-ci>? #\b #\B)

#false

> (char>? #\b #\B)

#true

函数

(char-downcase c)  char

  c : char
生成对应的小写字符。
> (char-downcase #\T)

#\t

函数

(char-lower-case? c)  boolean?

  c : char
判断字符是否是小写的。
> (char-lower-case? #\T)

#false

函数

(char-numeric? c)  boolean?

  c : char
判断字符是否表示数字。
> (char-numeric? #\9)

#true

函数

(char-upcase c)  char

  c : char
生成对应的大写字符。
> (char-upcase #\t)

#\T

函数

(char-upper-case? c)  boolean?

  c : char
判断字符是否是大写的。
> (char-upper-case? #\T)

#true

函数

(char-whitespace? c)  boolean?

  c : char
判断字符是否表示空格。
> (char-whitespace? #\tab)

#true

函数

(char<=? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否按增加的顺序排列。
> (char<=? #\a #\a #\b)

#true

函数

(char<? x d e ...)  boolean?

  x : char
  d : char
  e : char
判断字符是否按严格增加的顺序排列。
> (char<? #\a #\b #\c)

#true

函数

(char=? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否相等。
> (char=? #\b #\a)

#false

函数

(char>=? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否按减少的顺序排列。
> (char>=? #\b #\b #\a)

#true

函数

(char>? c d e ...)  boolean?

  c : char
  d : char
  e : char
判断字符是否按严格减少的顺序排列。
> (char>? #\A #\z #\a)

#false

函数

(char? x)  boolean?

  x : any/c
判断值是否为字符。
> (char? "a")

#false

> (char? #\a)

#true

1.11 字符串

函数

(explode s)  (listof string)

  s : string
将字符串转换为单字母字符串的表。
> (explode "cat")

(list "c" "a" "t")

函数

(format f x ...)  string

  f : string
  x : any/c
格式化字符串,同时可以将值嵌入其中。
> (format "Dear Dr. ~a:" "Flatt")

"Dear Dr. Flatt:"

> (format "Dear Dr. ~s:" "Flatt")

"Dear Dr. \"Flatt\":"

> (format "the value of ~s is ~a" '(+ 1 1) (+ 1 1))

"the value of (+ 1 1) is 2"

函数

(implode l)  string

  l : list?
将单字母字符串的表连接成一个字符串。
> (implode (cons "c" (cons "a" (cons "t" '()))))

"cat"

函数

(int->string i)  string

  i : integer
将[0,55295]或[57344,1114111]中的整数转换为单个字母的字符串。
> (int->string 65)

"A"

函数

(list->string l)  string

  l : list?
将字符的表转换为字符串。
> (list->string (cons #\c (cons #\a (cons #\t '()))))

"cat"

函数

(make-string i c)  string

  i : natural-number
  c : char
生成一个长度为i,内容为c的字符串。
> (make-string 3 #\d)

"ddd"

函数

(replicate i s)  string

  i : natural-number
  s : string
重复s i次。
> (replicate 3 "h")

"hhh"

函数

(string c ...)  string?

  c : char
用输入的字符构建字符串。
> (string #\d #\o #\g)

"dog"

函数

(string->int s)  integer

  s : string
将单个字母的字符串转换为[0,55295]或[57344,1114111]中的整数。
> (string->int "a")

97

函数

(string->list s)  (listof char)

  s : string
将字符串转换为字符的表。
> (string->list "hello")

(list #\h #\e #\l #\l #\o)

函数

(string->number s)  (union number #false)

  s : string
将字符串转换为数值,如果不可能则生成false。
> (string->number "-2.03")

#i-2.03

> (string->number "1-2i")

1-2i

函数

(string->symbol s)  symbol

  s : string
将字符串转换为符号。
> (string->symbol "hello")

'hello

函数

(string-alphabetic? s)  boolean?

  s : string
判断字符串中的所有“字母”是否都是字母。
> (string-alphabetic? "123")

#false

> (string-alphabetic? "cat")

#true

函数

(string-append s ...)  string

  s : string
连接几个字符串中的字符。
> (string-append "hello" " " "world" " " "good bye")

"hello world good bye"

函数

(string-ci<=? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否按字典顺序递增排列,不区分大小写。
> (string-ci<=? "hello" "WORLD" "zoo")

#true

函数

(string-ci<? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否按字典顺序严格递增排列,不区分大小写。
> (string-ci<? "hello" "WORLD" "zoo")

#true

函数

(string-ci=? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断所有字符串是否相等,字符对字符,不区分大小写。
> (string-ci=?  "hello" "HellO")

#true

函数

(string-ci>=? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否按字典顺序递减排列,不区分大小写。
> (string-ci>?  "zoo" "WORLD" "hello")

#true

函数

(string-ci>? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否按字典顺序严格递减排列,不区分大小写。
> (string-ci>?  "zoo" "WORLD" "hello")

#true

函数

(string-contains-ci? s t)  boolean?

  s : string
  t : string
判断第一个字符串是否出现在第二个字符串中,不考虑字母的大小写。
> (string-contains-ci? "At" "caT")

#true

函数

(string-contains? s t)  boolean?

  s : string
  t : string
判断第一个字符串是否出现在第二个字符串中。
> (string-contains? "at" "cat")

#true

函数

(string-copy s)  string

  s : string
复制字符串。
> (string-copy "hello")

"hello"

函数

(string-downcase s)  string

  s : string
生成和输入字符串类似的字符串,所有“字母”全部小写。
> (string-downcase "CAT")

"cat"

> (string-downcase "cAt")

"cat"

函数

(string-ith s i)  1string?

  s : string
  i : natural-number
提取s中的第i个字符。
> (string-ith "hello world" 1)

"e"

函数

(string-length s)  nat

  s : string
计算字符串的长度。
> (string-length "hello world")

11

函数

(string-lower-case? s)  boolean?

  s : string
判断字符串中的所有“字母”是否都是小写。
> (string-lower-case? "CAT")

#false

函数

(string-numeric? s)  boolean?

  s : string
判断字符串中的所有“字母”是否都是数字。
> (string-numeric? "123")

#true

> (string-numeric? "1-2i")

#false

函数

(string-ref s i)  char

  s : string
  i : natural-number
s中提取第i个字符。
> (string-ref "cat" 2)

#\t

函数

(string-upcase s)  string

  s : string
生成和输入字符串类似的字符串,所有“字母”全部大写。
> (string-upcase "cat")

"CAT"

> (string-upcase "cAt")

"CAT"

函数

(string-upper-case? s)  boolean?

  s : string
判断字符串中的所有“字母”是否都是大写。
> (string-upper-case? "CAT")

#true

函数

(string-whitespace? s)  boolean?

  s : string
判断字符串中的所有“字母”是否都是空格。
> (string-whitespace? (string-append " " (string #\tab #\newline #\return)))

#true

函数

(string<=? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否以按字典顺序递增排列。
> (string<=? "hello" "hello" "world" "zoo")

#true

函数

(string<? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否以按字典顺序严格递增排列。
> (string<? "hello" "world" "zoo")

#true

函数

(string=? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断所有字符串是否相等,字符对字符。
> (string=? "hello" "world")

#false

> (string=? "bye" "bye")

#true

函数

(string>=? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否以按字典顺序递减排列。
> (string>=?  "zoo" "zoo" "world" "hello")

#true

函数

(string>? s t x ...)  boolean?

  s : string
  t : string
  x : string
判断字符串是否以按字典顺序严格递减排列。
> (string>?  "zoo" "world" "hello")

#true

函数

(string? x)  boolean?

  x : any/c
判断值是否为字符串。
> (string? "hello world")

#true

> (string? 42)

#false

函数

(substring s i j)  string

  s : string
  i : natural-number
  j : natural-number
提取从ij(如果没有提供j,就是到字符串尾)的子字符串。
> (substring "hello world" 1 5)

"ello"

> (substring "hello world" 4)

"o world"

1.12 图像

函数

(image=? i j)  boolean?

  i : image
  j : image
判断两个图像是否相等。
> c1

image

> (image=? (circle 5 "solid" "green") c1)

#false

> (image=? (circle 10 "solid" "green") c1)

#true

函数

(image? x)  boolean?

  x : any/c
判断值是否为图像。
> c1

image

> (image? c1)

#true

1.13 杂项

函数

(=~ x y eps)  boolean?

  x : number
  y : number
  eps : non-negative-real
检查xy是相距在eps之内。
> (=~ 1.01 1.0 0.1)

#true

> (=~ 1.01 1.5 0.1)

#false

eof : eof-object?

表示文件结束的值:
> eof

#<eof>

函数

(eof-object? x)  boolean?

  x : any/c
判断某个值是否是文件结束值。
> (eof-object? eof)

#true

> (eof-object? 42)

#false

函数

(eq? x y)  boolean?

  x : any/c
  y : any/c
从计算机的角度判断两个值是否(内涵)相等。
> (eq? (cons 1 '()) (cons 1 '()))

#false

> one

(list 1)

> (eq? one one)

#true

函数

(equal? x y)  boolean?

  x : any/c
  y : any/c
判断两个值在结构上是否相等,其中基本值以eqv?谓词进行比较。
> (equal? (make-posn 1 2) (make-posn (- 2 1) (+ 1 1)))

#true

函数

(equal~? x y z)  boolean?

  x : any/c
  y : any/c
  z : non-negative-real
equal?比较xy是否相等,但在数值的情况下使用=~比较。
> (equal~? (make-posn 1.01 1.0) (make-posn 1.01 0.99) 0.2)

#true

函数

(eqv? x y)  boolean?

  x : any/c
  y : any/c
判断两个值是否(外延)相等,即从所用作用于其的函数角度来看。
> (eqv? (cons 1 '()) (cons 1 '()))

#false

> one

(list 1)

> (eqv? one one)

#true

函数

(error x ...)  void?

  x : any/c
抛出错误,错误消息由输入值组合而成。如果任何输入值的打印表示太长的话,它会被截断并以“...”的形式放入字符串中。如果第一个值是个符号,它将被放在错误消息前部并后跟冒号。
> zero

0

> (if (= zero 0) (error "can't divide by 0") (/ 1 zero))

can't divide by 0

函数

(exit)  void

(exit)求值就会终止正在运行的程序。

函数

(identity x)  any

  x : any/c
返回x
> (identity 42)

42

> (identity c1)

image

> (identity "hello")

"hello"

函数

(struct? x)  boolean?

  x : any/c
判断某个值是否为结构体。
> (struct? (make-posn 1 2))

#true

> (struct? 43)

#false