Using Let in Scheme

我怕爱的太早我们不能终老 提交于 2019-11-30 05:11:14

Use let* instead of let.

The difference between let and let* is the following:

let* binds variables from left to right. Earlier bindings can be used in new binding further to the right (or down).

let on the other hand can be thought of as syntactic sugar (or macro) for simple lambda abstraction:

(let ((a exp1)
      (b exp2))
   exp)

is equivalent to

((lambda (a b)
    exp)
 exp1 exp2)
  • 4ac is a variable with a numeric value, so (4ac) is not meaningful.

  • LET binds all variables, but the variables can't be used in the computations for the values.

This does not work:

(let ((a 1) (b 1) (c (* a b)))
   c)

Use:

(let ((a 1) (b 1))
  (let ((c (* a b)))
    c))

Above introduces A and B with the first LET. In the second LET both A and B now can be used to compute C.

Or:

(let* ((a 1) (b 1) (c (* a b)))
   c)

You'll need a special let-construct (let*) here since the variables inside the let-definition refer to each other.

It's rather a problem of defining a scope than of evaluating an expression (In usual let-definitions, the order of evaluation doesn't matter since the values may not use each other)

When you use let, the bindings are not visible in any of the bodies. Use let* instead and see the RNRS docs for details.

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