gpt4 book ai didi

algorithm - 方案功能

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:13:58 25 4
gpt4 key购买 nike

我对方案功能有疑问:

> (let ((x 2) (y 3))
> (let ((x 7) (z (+ x y)))
> (* z x) ) )

结果是:35。有人能解释一下为什么吗?我不明白的是,我们将 x 的值更改为 7,我认为结果应该是 70。

最佳答案

实际上,Sylwester 已经解释过了。

; I like to write `let` forms in this way:
(let ((x 2)
(y 3))
(let ((x 7)
(z (+ x y))) ; since this is within a `let` definition part,
; and not in a `let*` definition part,
; the definition of z - which is (+ x y) -
; cannot "see" (x 7) yet,
; so it "searches" for x and "finds" it above
; in the let definition (x 2).
; therefore, (z (+ x y)) will be evaluated as
; (z (+ 2 3)) thus (z 5)
(* z x))) ; this now is in the inner-let body,
; thus it "sees" (x 7) which has "shadowed" (x 2)
; therefore this will be evaluated as (* 5 7), thus 35.

关于algorithm - 方案功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52707432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com