将 a 转换cond
为嵌套if
的 s 后,您始终可以将其转换为and
or
如下not
所示:
(if A B C) --> (or (and A B) (and (not A) C))
但是,如果你盲目地这样做,你会得到一个比你能得到的更复杂的表达式,所以我会添加更多可以使用的转换:
(if A B #f) --> (and A B)
(if A B #t) --> (or (not A) B)
(if A #f C) --> (and (not A) C)
(if A #t C) --> (or A C)
(注意:or
上面可能会返回不同的真值而不是#t
,使其在技术上不同但在用作布尔值时等效)
我应该注意的另一件事是,有时您可以将多分支cond
转换为and
or
not
,而无需先转换为if
s。例如一个 3 分支cond
:
(cond [A B]
[C D]
[else E])
-->
(or (and A B)
(and (not A) C D)
(and (not A) (not C) E))
或 4 个分支cond
:
(cond [A B]
[C D]
[E F]
[else G])
-->
(or (and A B)
(and (not A) C D)
(and (not A) (not C) E F)
(and (not A) (not C) (not E) G))
每个都and
对应一个 cond-branch,每个 cond-branch除了它自己的条件外,对于每个先前的条件and
都有s。not
您可以应用更通用的规则:
for i from 1 through n,
(cond [Q_i A_i]
...
[else E])
-->
on each i, for j from 1 through i-1,
(or (and (not Q_j) ... Q_i A_i)
...
(and (not Q_i) ... E)