让混淆到lambda转换 [英] Confused by let to lambda conversion

查看:129
本文介绍了让混淆到lambda转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在查看关于Y组合器的精彩文章 ="http://users.cms.caltech.edu/~mvanier/" rel ="nofollow noreferrer">迈克·范尼尔.按照说明,以下行被删除:

I am currently, going through this great article on Y-combinator by Mike Vanier. Along the explanation the following line is dropped:

事实证明,可以使用以下等式将任何let表达式转换为等效的lambda表达式:

It turns out that any let expression can be converted into an equivalent lambda expression using this equation:

(let ((x <expr1>)) <expr2>) ==> ((lambda (x) <expr2>) <expr1>)

(let ((x <expr1>)) <expr2>) ==> ((lambda (x) <expr2>) <expr1>)

本文通过转换来说明此语句:

The article illustrates this statement by converting:

(define (part-factorial self)
  (let ((f (self self)))
    (lambda (n)
      (if (= n 0)
        1
        (* n (f (- n 1)))))))

收件人:

(define (part-factorial self)
  ((lambda (f)
    (lambda (n)
      (if (= n 0)
        1
        (* n (f (- n 1))))))
  (self self)))

现在,我理解了上面两个代码片段的方式和原因相同,尽管我无法理解将let转换为lambda的一般公式是这样的事实:

Now, I understand how and why two code snippets above are identical, though I can't get my head around the fact that general equation for converting let to lambda is:

(let ((x <expr1>)) <expr2>)
==> ((lambda (x) <expr2>) <expr1>)

我很感谢详尽的解释.

推荐答案

let使您可以打开一个可用变量的新环境.用编程语言来讲,我们说它打开了一个新的框架".

let lets you open a new environment in which variables are available. In programming language terms we say it "opens a new frame".

编写(let ((x 42)) <body>)时,将创建一个框架,其中<body>内的x可用,并为其分配值42.

When you write (let ((x 42)) <body>) you create a frame in which x is available inside <body>, and assign it the value 42.

嗯,还有另一个工具可以让您打开新框架.实际上,它通常是您可以用来构建更多抽象构造的基本模块:它称为lambda.

Well, there is another tool that lets you open new frames. In fact, it's usually the basic brick with which you can build more abstract constructs: it is called lambda.

lambda打开一个新框架,其中的参数可供其主体使用. 编写(lambda (x) <body>)时,会将x用于该功能的<body>.

lambda opens a new frame in which its arguments are available to its body. When you write (lambda (x) <body>) you make x available to the <body> of the function.

lambdalet之间的唯一区别是let立即为x分配一个值,而lambda等待该值作为参数.

The only difference between lambda and let is that let immediately assigns a value to x, while lambda awaits the value as an argument.

因此,如果要使用lambda用直接分配的值包装<body>,则只需传递该值!

Therefore, if you want to wrap a <body> with a directly assigned value using lambda, you just have to pass that value!

((lambda (x) <body>) 42)

这完全等同于:

(let ((x 42)) <body>)

这篇关于让混淆到lambda转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆