Confused by the difference between let and let* in Scheme

前端 未结 2 743
清歌不尽
清歌不尽 2020-11-27 06:28

Can anyone explain the difference simply? I don\'t think I understand the concept from the textbooks/sites I have consulted.

2条回答
  •  抹茶落季
    2020-11-27 07:06

    If you use let, you can't reference other bindings which appear in the same let expression.

    For example, this won't work:

    (let ((x 10)
          (y (+ x 6))) ; error! unbound identifier: x
      y)
    

    But if you use let*, it is possible to refer to previous bindings which appear in the same let* expression:

    (let* ((x 10)
           (y (+ x 6))) ; works fine
      y)
    => 16
    

    It's all here in the documentation.

提交回复
热议问题