Scheme 中的标识符和绑定 - 如何解释函数?

发布于 2024-09-29 01:42:27 字数 498 浏览 4 评论 0原文

我正在阅读 DrRacket 文档 http://docs.racket-lang.org/guide/binding。 html

有一个函数

  (define f
    (lambda (append)
      (define cons (append "ugly" "confusing"))
      (let ([append 'this-was])
        (list append cons))))
  > (f list)
  '(this-was ("ugly" "confusing"))

我看到我们定义了函数f,里面我们定义了需要(append)的lambda,为什么? lambda 的过程(主体)是另一个名为 cons 的函数,它附加两个字符串。

我根本不明白这个功能。 谢谢 !

I am reading DrRacket document http://docs.racket-lang.org/guide/binding.html

There is a function

  (define f
    (lambda (append)
      (define cons (append "ugly" "confusing"))
      (let ([append 'this-was])
        (list append cons))))
  > (f list)
  '(this-was ("ugly" "confusing"))

I see that we define function f, inside we define lambda that takes (append), why ?
Procedure (body) for lambda is another function called cons, that appends two strings.

I don't understand this function at all.
Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

自由如风 2024-10-06 01:42:27

您所指的部分演示了 Racket 中的词汇范围。与其他方案实现一样,要点是您可以“隐藏”语言中的每个绑定。与大多数“主流”语言不同,不存在真正的“神圣”关键字,因为它们永远不会被本地绑定所掩盖。

请注意,DrRacket 的“检查语法”按钮是一个非常好的可视化绑定内容的工具:单击它,您将看到代码中突出显示哪些部分是绑定,哪些是特殊形式 - 如果您将鼠标悬停在将鼠标悬停在特定名称上,您会看到一个箭头,告诉您它的来源。

The section that you're referring to demonstrates lexical scope in Racket. As in other Scheme implementations, the main point is that you can "shadow" every binding in the language. Unlike most "mainstream" languages, there are no real keywords that are "sacred" in the sense that they can never be shadowed by a local binding.

Note that a really good tool to visualize what is bound where is DrRacket's "check syntax" button: click it, and you'll see your code with highlights that shows which parts are bindings, which are special forms -- and if you hover the mouse over a specific name, you'll see an arrow that tells you where it came from.

笑,眼淚并存 2024-10-06 01:42:27

方案需要一些时间来适应:)

  1. f 被分配给 lambda 返回的函数。
  2. lambda 定义带有参数的函数(称为append)。
  3. (define cons (append "ugly" "confusing")) 本身不是一个函数,而是以两个字符串作为参数调用append,并将结果赋给cons。
  4. let 块内,append 被重新分配一个不同的值,即符号 this-was
  5. let 块创建一个 append 列表(现在包含 'this-was)和 cons (其中包含 '("ugly " "confusing") 来自上面的 3,
  6. 因为 5 是最后一个语句,该值由整个函数返回,该
  7. 函数被称为 f f 使用参数调用listlist 函数)作为参数append 传递,这就是为什么上面的3 创建了一个列表'("ugly" "confusing")。 被分配给 cons

希望能澄清一些事情。
干杯!

Scheme takes some getting used to :)

  1. f is assigned the function returned by the lambda.
  2. lambda defines the function that takes a parameter (called append).
  3. (define cons (append "ugly" "confusing")) is not a function per se, but calls append with the two strings as parameter and assigns the result to cons.
  4. inside the let block, append is re-assigned a different value, the symbol this-was.
  5. the let block creates a list of append (which now contains 'this-was) and cons (which contains '("ugly" "confusing") from 3 above
  6. since 5 is the last statement that value is returned by the whole function which is called f
  7. f is called with the parameter list (the list function). which gets passed as the parameter append. And this is why 3 above creates a list '("ugly" "confusing") which gets assigned to cons.

Hope that cleared up things a bit.
Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文