“let”在Scheme 中如何工作?

发布于 2024-11-03 04:54:48 字数 474 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎夜雨 2024-11-10 04:54:48

虽然不完全是您遇到的问题,但根据您对评估参数的顺序的疑问,let 也是 lambda 的“语法糖”,后跟首先评估的参数然后传递给 lambda,然后对其进行求值。

例如:

(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))

因此,如果您想知道求值顺序,则在求值主体之前对参数进行完全求值(并且一个参数不能引用它之前的参数...使用 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:

(let ((a (list 1 2 3))
      (b (list 4 5 6)))
     (cons a b))

is the same as:

((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).

你与清晨阳光 2024-11-10 04:54:48

您需要在 let 声明周围放置另一组括号:

(define (case-one-helper str)
  (let ((temp (substring str (+ 3 (string-contains str "my")))))
    (substring temp (string-contains temp " "))))

You need to put another set of parentheses around your let declarations:

(define (case-one-helper str)
  (let ((temp (substring str (+ 3 (string-contains str "my")))))
    (substring temp (string-contains temp " "))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文