if-statement - SICP 打算如何在没有替代 (else) 部分的情况下使用 (if) 表格?

标签 if-statement scheme sicp

Exercise 1.22 of SICP包含以下过程:

(define (start-prime-test n start-time)
  (if (prime? n)
      (report-prime (- (runtime) start-time))))

在我看来,if 表单没有替代分支。我只能看到 if (test) (consequent)。 SICP在哪里引入这种形式? ifSection 1.1.6 中引入但我在那个部分没有看到任何地方涵盖“if without else”的情况。我有可能从同一部分中 cond 的定义中猜出预期的行为,但该部分有 a footnote这表明两者是不同的。那么我如何才能知道 SICP 打算如何使用这种 if 形式呢?

最佳答案

查阅了索引,发现有两个地方定义了:Footnote 29在第 3.3 节和 Footnote 10在第 4.1 节中。引用两者:

Observe that the if expression in this procedure has no < alternative > expression. Such a ``one-armed if statement'' is used to decide whether to do something, rather than to select between two expressions. An if expression returns an unspecified value if the predicate is false and there is no < alternative >.

The value of an if expression when the predicate is false and there is no alternative is unspecified in Scheme; we have chosen here [in The Metacircular Evaluator] to make it false. We will support the use of the variables true and false in expressions to be evaluated by binding them in the global environment. See section 4.1.4.

我承认我应该在查阅 Stack Overflow 之前考虑查阅索引。显然,互联网已经腐 eclipse 了我的大脑。但是,我希望这个问题有比我在这里给出的更好的答案。 “它在你需要它之​​后定义了两章”,即使是正确的,也不是很愉快。

从好的方面来说,codybartfast 的评论正确地指出您不需要理解练习 1.22 中描述的过程来解决问题。事实上,我自己解决了这个问题,很明显我想得太多了。不到五行就可以解决。此外,也有人指出,虽然它的前几个词恰好符合 Scheme 的标准,但上面的第二个引用主要是在谈论第 4 章中正在构建的实现,而不是任何预期使用或在前面的章节中理解。

关于if-statement - SICP 打算如何在没有替代 (else) 部分的情况下使用 (if) 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64753765/

相关文章:

scheme - 评估组合以递归处理 (+ 1 2)

shell - 像快速失败测试一样运行 SICP Scheme 文件

java - 在另一个 if 语句中使用 if 语句的结果

c++ - 尝试使用模数或 fmod,但两者都给我的程序带来问题?

Excel公式: If condition then do that condition

programming-languages - 有人以 Scheme 编程语言为生吗?

iterator - 这个Scheme列表迭代器如何使用call-with-current-continuation?

functional-programming - 如何在 Scheme/LISP 中编写除一体功能外的所有功能?

java - 关于 IF 语句

recursion - 在 Clojure 中实现 foreach (doseq)