Common Lisp:将 IF 替换为 AND & OR?

Common Lisp: Replace IF with AND & OR?

我坚持练习 4.28。对符号计算的简要介绍(第 129 页):

We can usually rewrite an IF as a combination of AND plus OR by following this simple scheme: Replace (IF test true-part false-part) with the equivalent expression (OR (AND test true-part) false-part). But this scheme fails for the expression (IF (ODDP 5) (EVENP 7) ’FOO). Why does it fail? Suggest a more sophisticated way to rewrite IF as a combination of ANDs and ORs that does not fail.

(or (and (oddp 5) (or (evenp 7) t)) 'foo) 评估 true-part 并停止,如果 [=18] 它将评估 false-part =]test 是 NIL,但如果 test 是 T,它总是 returns T,这不反映 IF 的行为。目前在书上所学的问题是否有正确的解法,或者此时有none的习题答案?

我不是在问这个练习有没有答案,我只是想继续找一个对我来说是否有意义。

你很接近。它失败了,因为 (EVENP 7)(结果)恰好是假值 NIL 然后整个 and 形式变成 NIL 并且 or 将评估备选方案.

是的。有一种方法可以解决这个问题,即使只知道你已经在问题中呈现的形式,但它可能不会在未来使用副作用(打印内容的表达式)。