“let”在Scheme 中如何工作?
我使用 let
创建一个临时变量,然后在下一条语句中使用这个临时变量。然而,DrScheme 抱怨道,
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 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
This is my code snippet:
(define (case-one-helper str)
(let (temp (substring str (+ 3 (string-contains str "my"))))
(substring temp (string-contains temp " "))))
I wonder if the value of variable created by let
has to be known at compiled time?
Edit
I've just figured out, missing ()
.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然不完全是您遇到的问题,但根据您对评估参数的顺序的疑问,
let
也是 lambda 的“语法糖”,后跟首先评估的参数然后传递给 lambda,然后对其进行求值。例如:
与:
因此,如果您想知道求值顺序,则在求值主体之前对参数进行完全求值(并且一个参数不能引用它之前的参数...使用
let*
对于需要这样的绑定的东西)。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.For instance:
is the same as:
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
声明周围放置另一组括号:You need to put another set of parentheses around your
let
declarations: