方案中的 lambda 有何意义?
我正在学习计划。我知道如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
let
是一个lambda
。Eg
可以翻译为
此外,在Scheme 中,所有控制和环境结构都可以用lambda 表达式和lambda 的应用来表示。
因此,
lambda
严格来说比let
更强大,并且构成了Scheme 中许多有趣结构的基础。对于
define
和lambda
,顶级define
添加到顶级环境的绑定。当你写的时候
,你实际上是在说
嵌套定义被翻译成
letrec
,它也可以使用 lambda 重写。因此,再次强调,许多Scheme 构造都可以使用
lambda
转换为某种东西,因此很好地理解lambda
确实是值得的。A
let
is alambda
.E.g.
can be translated into
Furthermore, in Scheme all control and environment structures can be represented by lambda expressions and applications of lambdas.
So,
lambda
is strictly more powerful thanlet
and forms the basis of many of the interesting constructs found in Scheme.Concerning
define
andlambda
, a top-leveldefine
adds a binding to the top-level environment.When you write
you are really saying
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 understandlambda
well.如果您想创建一个函数并将其用作另一个函数的参数(例如
map
),但您实际上不想命名该函数,则可以使用 lambda 功能。例如,如果您想将 42 添加到列表中的每个数字,您可以这样做:
但如果您不想为只使用一次的函数命名,您可以这样做:
You use
lambda
if you want to create a function to use it as an argument to another function (like for examplemap
), 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:
But if you don't want to give a name to a function that you only use this once, you could just do:
Let 实际上只是 Lambda 表达式的简写。
以下两个表达式是等效的:
Lambda 遵循语言的哲学,即一切都应该看起来像数学函数。但在实践中,如果变量太多,Let 可以更容易地弄清楚发生了什么。想象一下 10 个变量,它们的值在 Lambda 块之后定义,并且您尝试将每个变量与变量名称相匹配,让变量的值放在它们的名称旁边,这对程序员来说很方便,但不太符合函数式编程哲学。
Lambda 可用于从高阶函数返回函数,但 let 不能这样做。例如:
Lambda 还可以用于将函数传递给函数:
Let is actually just shorthand for a Lambda expression.
The following two expressions are equivalent:
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:
Lambda can also be used to pass a function to a function:
lambda 创建新的匿名函数,当然每次使用它们时都会对其进行评估。
let 为值创建临时名称,并设置一次以在 let 形式定义的范围内使用。
它们确实是非常不同的野兽。
一些例子:
第一种形式创建一个乘以 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 :
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
在Scheme中,过程(或函数)是第一类对象,如列表、数字或字符串。要创建列表文字,您可以使用名为
list
的原语:就像这样,要创建过程,您可以使用
lambda
原语(或特殊形式):作为抽象的主要形式,Scheme 为
define
提供了一个快捷方式,以便轻松绑定新过程:除此之外,过程就像所有其他第一类对象一样。它们可以作为参数传递给其他过程,并且一个过程可以计算以生成(或返回)另一个过程。
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
:Just like this, to create a procedure, you use the
lambda
primitive (or special form):As procedures are the primary form of abstraction, Scheme provides a shortcut for
define
to make it easy to bind new procedures: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.
你可以这样想...
您创建一个使用另一个函数的函数
但是你想让事情变得更加模块化,所以你要做的就是将第二个函数称为第一个函数的参数,这样当你觉得需要另一个功能时,你就可以更改第二个函数......
希望这是有道理的
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