灯下 登录
计算机科学 / SICP / 4.1.2 Representing Expressions

Exercise 4.4 · 习题

Exercise 4.4: Recall the definitions of the
special forms and and or from Chapter 1:

and: The expressions are evaluated from left to right. If any
expression evaluates to false, false is returned; any remaining expressions are
not evaluated. If all the expressions evaluate to true values, the value of
the last expression is returned. If there are no expressions then true is
returned.

or: The expressions are evaluated from left to right. If any expression
evaluates to a true value, that value is returned; any remaining expressions
are not evaluated. If all expressions evaluate to false, or if there are no
expressions, then false is returned.

Install and and or as new special forms for the evaluator by

defining appropriate syntax procedures and evaluation procedures

eval-and and eval-or. Alternatively, show how to implement

and and or as derived expressions.

练习 4.4:回忆第 1 章中 and 和 or 特殊形式的定义:

and:表达式从左到右依次求值。若某个表达式求值为假,则返回假,其余表达式不再求值。若所有表达式均求值为真值,则返回最后一个表达式的值。若没有表达式,则返回真。

or:表达式从左到右依次求值。若某个表达式求值为真值,则返回该值,其余表达式不再求值。若所有表达式均求值为假,或者没有表达式,则返回假。

通过定义适当的语法过程和求值过程 eval-and 与 eval-or,将 and 和 or 安装为求值器的新特殊形式。或者,说明如何将 and 和 or 实现为派生表达式。