2 初级+缩写的表
语法符号使用符号X ...(粗体的点)表示X可能出现任意多次(零次,一次或多次)。此外,语法还将...定义为可以在模板中使用的标识符。
关于初级语言的解释,请参阅《程序设计方法(第二版)》的独立章节1。
关于引用表的解释,请参阅《程序设计方法(第二版)》的独立章节2。
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 ...) | ||
| | (prim-op expr ...) | |||
| | (cond [expr expr] ... [expr expr]) | |||
| | (cond [expr expr] ... [else expr]) | |||
| | (if expr expr expr) | |||
| | (and expr expr expr ...) | |||
| | (or expr expr expr ...) | |||
| | name | |||
| | ’quoted | |||
| | ‘quasiquoted | |||
| | ’() | |||
| | number | |||
| | boolean | |||
| | string | |||
| | character | |||
quoted | = | name | ||
| | number | |||
| | string | |||
| | character | |||
| | (quoted ...) | |||
| | ’quoted | |||
| | ‘quoted | |||
| | ,quoted | |||
| | ,@quoted | |||
quasiquoted | = | name | ||
| | number | |||
| | string | |||
| | character | |||
| | (quasiquoted ...) | |||
| | ’quasiquoted | |||
| | ‘quasiquoted | |||
| | ,expr | |||
| | ,@expr | |||
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) |
2.1 预定义变量
2.2 模板变量
语法
语法
语法
语法
语法
2.3 初级+缩写的表的语法
语法
‘name
语法
‘part
语法
(quasiquote name)
语法
(quasiquote part)
语法
,expression
语法
(unquote expression)
在多个quasiquote中,,expression只是文本的,expression,expression的quasiquote层数将被减一。
语法
,@expression
语法
(unquote-splicing expression)
在多个quasiquote中,拼接的unquote就和unquote一样;即,将quasiquote的层数减一。
通常,拼接的unquote由,写出,但也可以用unquote-splicing来写。
2.4 通用的语法
以下语法在初级+缩写的表中的行为和初级中相同。
语法
(define (name variable variable ...) expression)
语法
(define name expression)
除了这种语法之外,不能使用lambda。
语法
(define-struct structure-name (field-name ...))
make-structure-name :读入和结构体字段数一样多的参数,并创建结构体的新实例。
structure-name-field-name :读入结构体实例,返回名为field-name字段的值。
structure-name? :读入任意值,如果该值是结构体的实例就返回#true 。
由define-struct 引入的新函数名必须不同与其他函数或变量,否则define-struct 会报告错误。
语法
(name expression expression ...)
语法
(cond [question-expression answer-expression] ...)
(cond [question-expression answer-expression] ... [else answer-expression])
如果没有question-expression的计算结果为#true ,那么cond 的值是else 子句中的answer-expression。如果不存在else 子句,cond 报告错误。如果某个question-expression的值既不是#true 也不是#false ,cond 也报告错误。
语法
(if question-expression then-answer-expression else-answer-expression)
语法
(and expression expression expression ...)
语法
(or expression expression expression ...)
语法
(check-expect expression expected-expression)
(check-expect (fahrenheit->celsius 212) 100) (check-expect (fahrenheit->celsius -40) -40) (define (fahrenheit->celsius f) (* 5/9 (- f 32)))
语法
(check-random expression expected-expression)
check-random为其两个部分提供相同的随机数生成器。如果两者以相同的顺序从相同的区间中取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)))
; 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)))
语法
(check-satisfied expression predicate)
> (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
; [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))))]))
> (check-satisfied (htdp-sort (list 1 2 0 3)) sorted?)
语法
(check-within 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 (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!
> (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
expressions或expected-expression返回函数是一种错误;详情参见check-expect的说明。
语法
(check-error expression expected-error-message)
(check-error expression)
(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 ...)
; [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!
语法
(check-range expression low-expression high-expression)
; [Real -> Real] Real -> Real ; f在x的斜率是多少? (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)
语法
(require string)
语法
(require module-name)
语法
(require (lib string string ...))
语法
(require (planet string (string string number number)))
语法
(require (planet id))
语法
(require (planet string))
planet requires的完整语法在Importing and Exporting: require and provide中给出,但找到语法示例的最佳位置位于PLaneT 服务器上特定package的描述中。
2.5 预定义函数
后续小节列出了编程语言中内置的函数。所有其他函数要么从教学包中导入,要么必须在程序中定义。
2.6 数值:整数、有理数、实数、复数、精确数、非精确数
函数
(* 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 3 4) #i0.6435011087932844
> (atan -2 -1) #i-2.0344439357957027
函数
(ceiling x) → integer
x : real
> (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) 1583484523
函数
(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
> (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
> (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?
> (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
> (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) 12
函数
(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
函数
(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
> (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
2.7 布尔值
函数
(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) #true
函数
(not x) → boolean?
x : boolean?
> (not #false) #true
2.8 符号
函数
(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
2.9 链表
函数
(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)
> (assoc "hello" '(("world" 2) ("hello" 3) ("good" 0))) (list "hello" 3)
函数
(assq x l) → (union #false cons?)
x : any/c l : list?
> a (list (list 'a 22) (list 'b 8) (list 'c 70))
> (assq 'b a) (list 'b 8)
函数
(caaar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) #true) 42)
> (caaar w) (list "bye")
函数
(caadr x) → any/c
x : list?
> (caadr (cons 1 (cons (cons 'a '()) (cons (cons 'd '()) '())))) 'a
函数
(caar x) → any/c
x : list?
> y (list (list (list 1 2 3) #false "world"))
> (caar y) (list 1 2 3)
函数
(cadar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) #true) 42)
> (cadar w) #true
函数
(cadddr x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (cadddr v) 4
函数
(caddr x) → any/c
x : list?
> x (list 2 "hello" #true)
> (caddr x) #true
函数
(cadr x) → any/c
x : list?
> 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?
> w (list (list (list (list "bye") 3) #true) 42)
> (cdaar w) (list 3)
函数
(cdadr x) → any/c
x : list?
> (cdadr (list 1 (list 2 "a") 3)) (list "a")
函数
(cdar x) → list?
x : list?
> y (list (list (list 1 2 3) #false "world"))
> (cdar y) (list #false "world")
函数
(cddar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) #true) 42)
> (cddar w) '()
函数
(cdddr x) → any/c
x : list?
> 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?
> 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 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
> (make-list 3 "hello") (cons "hello" (cons "hello" (cons "hello" '())))
函数
(member x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" #true)
> (member "hello" x) #true
函数
(member? x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" #true)
> (member? "hello" x) #true
函数
(memq x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" #true)
> (memq (list (list 1 2 3)) x) #false
函数
(memq? x l) → boolean?
x : any/c l : list?
> 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 (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
> (range 0 10 2) (cons 0 (cons 2 (cons 4 (cons 6 (cons 8 '())))))
函数
(remove x l) → list?
x : any/c l : list?
> 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?
> 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
2.10 Posn
函数
(make-posn x y) → posn
x : any/c y : any/c
> (make-posn 3 3) (make-posn 3 3)
> (make-posn "hello" #true) (make-posn "hello" #true)
函数
(posn-x p) → any
p : posn
> p (make-posn 2 -3)
> (posn-x p) 2
函数
(posn-y p) → any
p : posn
> p (make-posn 2 -3)
> (posn-y p) -3
函数
(posn? x) → boolean?
x : any/c
> q (make-posn "bye" 2)
> (posn? q) #true
> (posn? 42) #false
2.11 字符
函数
(char->integer c) → integer
c : char
> (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
2.12 字符串
函数
(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
> (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
> (make-string 3 #\d) "ddd"
函数
(replicate i s) → string
i : natural-number s : string
> (replicate 3 "h") "hhh"
函数
(string c ...) → string?
c : char
> (string #\d #\o #\g) "dog"
函数
(string->int s) → integer
s : string
> (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
> (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
> (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
> (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
> (substring "hello world" 1 5) "ello"
> (substring "hello world" 4) "o world"
2.13 图像
函数
(image=? i j) → boolean?
i : image j : image
> c1 > (image=? (circle 5 "solid" "green") c1) #false
> (image=? (circle 10 "solid" "green") c1) #true
函数
(image? x) → boolean?
x : any/c
> c1 > (image? c1) #true
2.14 杂项
函数
(=~ x y eps) → boolean?
x : number y : number eps : non-negative-real
> (=~ 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
> (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~? (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
函数
(identity x) → any
x : any/c
> (identity 42) 42
> (identity c1) > (identity "hello") "hello"
函数
(struct? x) → boolean?
x : any/c
> (struct? (make-posn 1 2)) #true
> (struct? 43) #false