Ern*_*t A 2 lisp scheme let conditional-statements
通常,我发现我想根据先前值的值有条件地计算一系列值。例如,让我们把这些价值观x,y和z。首先我计算一下x。如果x满足某些条件,那么我将计算y,它是的函数x,依此类推。示意地,
;; compute value x
;; if x =? #f -> #f
;; else compute value y = f(x)
;; if y =? #f -> #f
;; else compute value z = f(y)
;; et cetera
Run Code Online (Sandbox Code Playgroud)
您如何在Scheme中做到这一点?我认为通常情况下会使用,cond但cond会丢弃测试结果,因此在这种情况下没有用。
使用let*,它在简短绑定的范围内顺序评估初始化形式。在初始化表格中,用于and使计算成为条件。
(let* ((x (compute-x))
(y (and x (f1 x)))
(z (and y (f2 y))))
;; code that uses the variables
)
Run Code Online (Sandbox Code Playgroud)