`let` 在 Scheme 中是如何工作的? [英] How does `let` work in Scheme?
问题描述
我使用 let
创建一个临时变量,然后在下一个语句中使用这个临时变量.然而,DrScheme 抱怨说,
I use let
to create a temporary variable, and then use this temporary variable in the next statement. However, DrScheme complained,
let: bad syntax (not an identifier and expression for a binding) in: temp
这是我的代码片段:
(define (case-one-helper str)
(let (temp (substring str (+ 3 (string-contains str "my"))))
(substring temp (string-contains temp " "))))
我想知道 let
创建的变量的值是否必须在编译时知道?
I wonder if the value of variable created by let
has to be known at compiled time?
编辑我刚刚发现,缺少 ()
.
谢谢,
推荐答案
虽然不完全是您遇到的问题,但基于您对评估参数的顺序的质疑,let
也是 lambda 的语法糖",其后是先求值然后传递给 lambda 的参数,然后再求值.
While not exactly the problem you're experiencing, but an aside based on your questioning about the sequence of evaluating the arguments, let
is also "syntactic sugar" for a lambda followed by it's arguments that are first evaluated and then passed to the lambda which is then evaluated.
例如:
(let ((a (list 1 2 3))
(b (list 4 5 6)))
(cons a b))
等同于:
((lambda (list-a list-b) (cons list-a list-b)) (list 1 2 3) (list 4 5 6))
因此,如果您想知道求值顺序,则在求值主体之前对参数进行完全求值(并且一个参数不能引用它前面的参数......需要这样绑定的东西).
So, if you're ever wondering about evaluation sequence, the arguments are evaluated fully before the body is evaluated (and one argument cannot refer to an argument preceding it ... use let*
for something that requires bindings like that).
这篇关于`let` 在 Scheme 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!