Scheme Confusing of Let and Let*

微笑、不失礼 提交于 2019-12-22 07:56:15

问题


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

With the code above, why is the answer 35, not 70? In the second let, x is 7 so z should be 7 + 3 = 10, and then the result should be 7 * 10 = 70. I know got another is let* I am very confusing between this 2. The sample is grabs from google. I already google but just can't get it.


回答1:


x is still bound to the outer let when calling (+ x y).




回答2:


To expand on Leppie's answer: if you had written

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

you would get the answer you expected. The internal let* is exactly equivalent to

(let ((x 7))
    (let ((z (+ x y)))
       (* z x)))

and in fact might be implemented that way in some Schemes.

In other words, in a let* form each successive binding after the first is in the scope of all the previously created bindings.



来源:https://stackoverflow.com/questions/8036840/scheme-confusing-of-let-and-let

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!