拉姆达表达式
有人能给我解释一下 lambda 表达式和 lambda 表达式吗?它们可以用来做什么。我已经用谷歌搜索过它&有一个大概的想法。大多数示例都给出了 C# 代码。普通旧 C 中的 lambda 表达式怎么样?
Can somebody explain me lambda expressions & what they can be used for. I have googled for it & have a rough idea. most of the examples give c# code. How about lambda expressions in plain old C...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上有两种称为“lambda 表达式”的东西,它们的相关性相当松散:
Lambda 表达式是 lambda 演算并且与函数式编程密切相关
在命令式语言中,lambda 表达式通常是匿名方法的同义词。例如,在 C# 中,您可以将 lambda 表达式(即表达式本身,而不仅仅是其结果)作为参数传递:
C#:
话虽如此,C 不支持匿名方法。但是,您可以使用函数指针来获得类似的结果:
There are actually two things called "lambda expressions", which are rather loosely related:
Lambda expressions are fundamental part of lambda calculus and are closely related to functional programming
In imperative languages, lambda expressions are usually synonyms for anonymous methods. In C#, for example you can pass lambda expression (ie. an expression itself, not just its result) as an argument:
C#:
Having said that, C does not support anonymous methods. You can, however, use function pointers to achieve similar results:
el.pescado 的答案是正确的,但他提供的示例有一个简单的解决方法,使用函数指针。 lambda 函数的许多用途无法用 c 的函数指针来解决。
假设你用 c 编写这些函数:
这些函数都很容易理解。现在假设您要编写一个函数,该函数接受 y 作为输入并返回指向函数 Multiply_y() 的指针:
其中“Multiply_y”是动态创建的函数,其形式为 Multiply_1、Multiply_2 等。 lambda 函数可以做到这一点。
el.pescado's answer is right but the example that he provides has an easy work around, using a function pointer. Many uses of lambda functions can't be solved with c's function pointers.
Say you write these functions in c:
Those are all pretty easy to understand. Now assume that you want to write a function that takes y as input and returns a pointer to the function Multiply_y():
Where "Multiply_y" is a dynamically created function of the form of Multiply_1, Multiply_2, etc. Languages that have first-class lambda functions can do that.
C 不支持 lba 表达式...如果您了解 perl,我强烈推荐《higher order perl》这本书,它将以熟悉的(如果您了解 perl)和实用的方式为您提供各种函数式编程技术的精彩介绍。环境。
C doesn't support lamba expressions...if you know perl, I highly recommend the book "higher order perl" which will give you a great introduction to all sorts of functional programming techniques in a familiar (if you know perl) and practical setting.
请参阅 MSDN
Look here on the MSDN