拉姆达表达式

发布于 2024-08-29 18:08:40 字数 114 浏览 2 评论 0原文

有人能给我解释一下 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 技术交流群。

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

发布评论

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

评论(4

我的奇迹 2024-09-05 18:08:41

实际上有两种称为“lambda 表达式”的东西,它们的相关性相当松散:

  1. Lambda 表达式是 lambda 演算并且与函数式编程密切相关

  2. 在命令式语言中,lambda 表达式通常是匿名方法的同义词。例如,在 C# 中,您可以将 lambda 表达式(即表达式本身,而不仅仅是其结果)作为参数传递:

C#:

someCollection.Apply (x => 2*x); // apply expression to every object in collection
// equivalent to 
someCollection.Apply (delegate (int x) { return 2 * X; });

话虽如此,C 不支持匿名方法。但是,您可以使用函数指针来获得类似的结果:

int multiply (int x)
{
    return 2 * x;
}

...
collection_apply (some_collection, multiply);

There are actually two things called "lambda expressions", which are rather loosely related:

  1. Lambda expressions are fundamental part of lambda calculus and are closely related to functional programming

  2. 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#:

someCollection.Apply (x => 2*x); // apply expression to every object in collection
// equivalent to 
someCollection.Apply (delegate (int x) { return 2 * X; });

Having said that, C does not support anonymous methods. You can, however, use function pointers to achieve similar results:

int multiply (int x)
{
    return 2 * x;
}

...
collection_apply (some_collection, multiply);
━╋う一瞬間旳綻放 2024-09-05 18:08:41

el.pescado 的答案是正确的,但他提供的示例有一个简单的解决方法,使用函数指针。 lambda 函数的许多用途无法用 c 的函数指针来解决。

假设你用 c 编写这些函数:

int Multiply_1(int x) { return(x*1); }
int Multiply_2(int x) { return(x*2); }
int Multiply_3(int x) { return(x*3); }
int Multiply_4(int x) { return(x*4); }
etcetera, to infinity

这些函数都很容易理解。现在假设您要编写一个函数,该函数接受 y 作为输入并返回指向函数 Multiply_y() 的指针:

(int)(int) *Make_Multiplier(int y) { return(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:

int Multiply_1(int x) { return(x*1); }
int Multiply_2(int x) { return(x*2); }
int Multiply_3(int x) { return(x*3); }
int Multiply_4(int x) { return(x*4); }
etcetera, to infinity

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():

(int)(int) *Make_Multiplier(int y) { return(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.

醉殇 2024-09-05 18:08:41

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.

要走干脆点 2024-09-05 18:08:41

请参阅 MSDN

Look here on the MSDN

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