阻止记号被求值
将符号原封不动地传递到程序,而不是求值后的其他内容
> (+ 2 3) 5 > (quote (+ 2 3)) (+ 2 3) > '(+ 2 3) (+ 2 3)
cons ,car ,cdr
一对 pairs 是 cons
cons
car 是前面的
car
cdr 是后面的
cdr
nil 是 empty
nil
if表达式
if
(if <predicate> <consequent> <alternative>)
and 和 or
and
or
(and <e1> ... <en>), (or <e1> ... <en>)
绑定符号。
(define <symbol> <expression>)
新程序。
(define (<symbol> <formal parameters>) <body>)
(lambda (<formal-parameters>) <body>)
举例:
(define (plus4 x) (+ x 4))
(define plus4 (lambda (x) (+ x 4)))
scheme 中只有 #f, false, False 代表假值
scheme
#f, false, False
=, eq?, equal?
= 只能用于比较数字。
=
eq?在Python中类似于 ==,用于比较两个 non-pairs (数字、布尔运算等)。否则,eq? 的行为就像在Python中的 is 一样。
eq?
==
non-pairs
is
equal? 比较 pairs, 通过比较它们的 cars 是否相等,它们的 cdrs 是否相等。否则,equal? 的行为就像 eq? 一样。
equal?
pairs
cars
cdrs
Last updated 5 years ago
scm> (eq? '(1 2 3) '(1 2 3)) #f scm> (equal? '(1 2 3) '(1 2 3)) #t scm> (= '(1 2 3) '(1 2 3)) Traceback (most recent call last): 0 (= (quote (1 2 3)) (quote (1 2 3))) Error: operand 0 ((1 2 3)) is not a number