方案中的 lambda 有何意义?

发布于 2024-09-03 18:32:20 字数 690 浏览 7 评论 0原文

我正在学习计划。我知道如何使用 lambda 和 let 表达式。

然而我正在努力弄清楚使用 lambda 的意义是什么。难道你不能用 let 做所有可以用 lambda 做的事情吗?

如果能看到 lambda 表达式是比 let 更好的选择的情况的示例,将会特别有帮助。

另一件事 - 是否也存在 let 比 lambda 更有用的情况?如果是这样的话,这样的例子也很好。

编辑:我也对对比 Define 和 lambda 感兴趣,因为它们似乎执行类似的任务。


更新:

感谢大家的帮助。阅读您的答案后,我对 lambda/let/define 进行了更多研究,现在更好地理解了它。

我遇到了一个很棒的 lambda 使用示例 - 从过程返回匿名函数。例如,下面的过程 operateTwice 返回一个基于传入过程的参数的匿名函数:

(define operateTwice
  (lambda (op1 op2)
    (lambda (x y)
      (op2 (op1 x y) y))))

((operateTwice * +) 2 3) ;equivalent to: (+ (* 2 3) 3), or in standard notation 2*3+3

输出:

9

I am learning scheme. I know how to use both lambda and let expressions.

However I'm struggling to figure out what the point is of using lambda. Can't you do everything with let that you can with lambda?

It would be especially helpful to see an example of a situation where a lambda expression is a better choice than let.

One other thing - are there also situations where let is more useful than lambda? If so such an example would be nice as well.

Edit: I'm also interested in contrasting define and lambda, as they seem to perform similar tasks.


Update:

Thanks for the help everyone. I did some more looking into lambda/let/define after reading your answers, and now understand it a lot better.

I came accross an excellent example of cool lambda useage - returning anonymous functions from procedures. For example, the procedure operateTwice below returns an anonymous function that is based on parameters passed in to the procedure:

(define operateTwice
  (lambda (op1 op2)
    (lambda (x y)
      (op2 (op1 x y) y))))

((operateTwice * +) 2 3) ;equivalent to: (+ (* 2 3) 3), or in standard notation 2*3+3

Output:

9

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

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

发布评论

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

评论(6

let 是一个lambda

Eg

(let ((x 1))
  body)

可以翻译为

((lambda (x) body) 1)

此外,在Scheme 中,所有控制和环境结构都可以用lambda 表达式和lambda 的应用来表示。

因此,lambda 严格来说比 let 更强大,并且构成了Scheme 中许多有趣结构的基础。

对于 definelambda,顶级 define 添加到顶级环境的绑定。

当你写的时候

(define (f x)
  body)

,你实际上是在说

(define f (lambda (x) body))

嵌套定义被翻译成 letrec,它也可以使用 lambda 重写。

因此,再次强调,许多Scheme 构造都可以使用lambda 转换为某种东西,因此很好地理解lambda 确实是值得的。

A let is a lambda.

E.g.

(let ((x 1))
  body)

can be translated into

((lambda (x) body) 1)

Furthermore, in Scheme all control and environment structures can be represented by lambda expressions and applications of lambdas.

So, lambda is strictly more powerful than let and forms the basis of many of the interesting constructs found in Scheme.

Concerning define and lambda, a top-level define adds a binding to the top-level environment.

When you write

(define (f x)
  body)

you are really saying

(define f (lambda (x) body))

Nested defines are translated into letrec, which can be rewritten using lambdas as well.

So, again, a lot of Scheme constructs can be translated into something using lambda, and therefore it is really worthwile that you understand lambda well.

始终不够 2024-09-10 18:32:20

如果您想创建一个函数并将其用作另一个函数的参数(例如 map),但您实际上不想命名该函数,则可以使用 lambda 功能。

例如,如果您想将 42 添加到列表中的每个数字,您可以这样做:

(define (add42 x) (+ x 42))
(map add42 (list 1 2 3 4))

但如果您不想为只使用一次的函数命名,您可以这样做:

(map (lambda (x) (+ x 42)) (list 1 2 3 4))

You use lambda if you want to create a function to use it as an argument to another function (like for example map), but you don't actually want to name the function.

For example, if you want to add 42 to every number in a list, you can do:

(define (add42 x) (+ x 42))
(map add42 (list 1 2 3 4))

But if you don't want to give a name to a function that you only use this once, you could just do:

(map (lambda (x) (+ x 42)) (list 1 2 3 4))
陈甜 2024-09-10 18:32:20

Let 实际上只是 Lambda 表达式的简写。
以下两个表达式是等效的:

(let ((alpha 7)) (* 5 alpha))

((lambda (alpha) (* 5 alpha)) 7)

Lambda 遵循语言的哲学,即一切都应该看起来像数学函数。但在实践中,如果变量太多,Let 可以更容易地弄清楚发生了什么。想象一下 10 个变量,它们的值在 Lambda 块之后定义,并且您尝试将每个变量与变量名称相匹配,让变量的值放在它们的名称旁边,这对程序员来说很方便,但不太符合函数式编程哲学。

Lambda 可用于从高阶函数返回函数,但 let 不能这样做。例如:

(define (plus-list x)
  (cond ((number? x)
         (lambda (y) (+ (sum-n x) y)))
        ((list? x)
         (lambda (y) (+ (sum-list x) y)))
        (else (lambda (x) x))
        ))

> ((plus-list 3) 4)
10
> ((plus-list '(1 3 5)) 5)
14
> ((plus-list 'a) 5)
5

Lambda 还可以用于将函数传递给函数:

>(map (lambda (x) (+ 1 x)) '(-1 2 -3))
(0 3 -2)

Let is actually just shorthand for a Lambda expression.
The following two expressions are equivalent:

(let ((alpha 7)) (* 5 alpha))

((lambda (alpha) (* 5 alpha)) 7)

Lambda follows the philosophy of the language that everything should look like a mathematical function. But in practice Let makes it easier to figure out what is happening if there are too many variables. Imagine 10 variables which have their values defined after the Lambda block, and you trying to match each of those with the variable name, with Let the values of variables are placed right next to their names, convenient for the programmer but conforming less to the Functional Programming philosophy.

Lambda can be used to return a function from a higher order function however let cannot do that. Eg:

(define (plus-list x)
  (cond ((number? x)
         (lambda (y) (+ (sum-n x) y)))
        ((list? x)
         (lambda (y) (+ (sum-list x) y)))
        (else (lambda (x) x))
        ))

> ((plus-list 3) 4)
10
> ((plus-list '(1 3 5)) 5)
14
> ((plus-list 'a) 5)
5

Lambda can also be used to pass a function to a function:

>(map (lambda (x) (+ 1 x)) '(-1 2 -3))
(0 3 -2)
弱骨蛰伏 2024-09-10 18:32:20

lambda 创建新的匿名函数,当然每次使用它们时都会对其进行评估。

let 为值创建临时名称,并设置一次以在 let 形式定义的范围内使用。

它们确实是非常不同的野兽。

一些例子:

(lambda (x) (* 5 x))

(让 ([x 2]) (* 5 x))
10
(让 ([f (lambda (x) (* 5 x))]) (f 2))
10

第一种形式创建一个乘以 5 的函数

第二种形式将 2 赋给 x 并将其乘以 5,得到 10

第三种我们使用 1 的函数(乘以 5)并以 2 作为参数调用它,也得到 10

lambda creates new anonymous functions, which of course are evaluated each time you use them.

let creates temporary names for values and are set once for use in the scope defined by the let form.

They are really very different beasts.

some examples :

(lambda (x) (* 5 x))

(let ([x 2]) (* 5 x))
10
(let ([f (lambda (x) (* 5 x))]) (f 2))
10

first form creates a function to multiply by 5

second form assign 2 to x and multiplies it by 5 resulting in 10

third we use the function of 1 (which mutiplies by 5) and call it with 2 as a parameter resulting also in 10

于我来说 2024-09-10 18:32:20

在Scheme中,过程(或函数)是第一类对象,如列表、数字或字符串。要创建列表文字,您可以使用名为 list 的原语:

> (define marks (list 33 40 56))
> marks
> (33 40 56)

就像这样,要创建过程,您可以使用 lambda 原语(或特殊形式)

> (define add-marks (lambda (m) (apply + m)))
> (add-marks marks)
> 129

:作为抽象的主要形式,Scheme 为 define 提供了一个快捷方式,以便轻松绑定新过程:

> (define (add-marks m) (apply + m))

除此之外,过程就像所有其他第一类对象一样。它们可以作为参数传递给其他过程,并且一个过程可以计算以生成(或返回)另一个过程。

In Scheme a procedure (or function) is a first class object like a list, a number or a string. To create a list literal you use the primitive called list:

> (define marks (list 33 40 56))
> marks
> (33 40 56)

Just like this, to create a procedure, you use the lambda primitive (or special form):

> (define add-marks (lambda (m) (apply + m)))
> (add-marks marks)
> 129

As procedures are the primary form of abstraction, Scheme provides a shortcut for define to make it easy to bind new procedures:

> (define (add-marks m) (apply + m))

Other than this, procedures are just like all other first class objects. They can be passed as arguments to other procedures and a procedure can evaluate to produce (or return) another procedure.

灯下孤影 2024-09-10 18:32:20

你可以这样想...
您创建一个使用另一个函数的函数
但是你想让事情变得更加模块化,所以你要做的就是将第二个函数称为第一个函数的参数,这样当你觉得需要另一个功能时,你就可以更改第二个函数......
希望这是有道理的

You can think it like that...
you create a function that uses another function
but you want to make things more modular so what you do is you call the second function as an argument of the first one, and that leaves you the possibility to change the second whenever you feel like you need another functionality...
hope that makes sense

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