什么是拉姆达?

发布于 2024-07-06 12:51:07 字数 84 浏览 4 评论 0原文

有人可以很好地描述什么是 Lambda 吗? 我们为它们设置了一个标签,它们涉及 C# 问题的秘密,但我还没有找到一个很好的定义和解释来解释它们是什么。

Could someone provide a good description of what a Lambda is? We have a tag for them and they're on the secrets of C# question, but I have yet to find a good definition and explanation of what they are in the first place.

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

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

发布评论

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

评论(7

静谧 2024-07-13 12:51:07

闭包、lambda 和匿名函数不一定是同一件事。

匿名函数是任何没有(或者至少不需要)自己名称的函数。

闭包是一个函数,它可以访问声明时位于其词法作用域内的变量,即使它们已经超出了作用域。 匿名函数不一定是闭包,但大多数语言中都有匿名函数,如果不是的话,它们的用处就会大大降低。

就计算机科学而言,lambda 的定义并不那么明确。 许多语言甚至不使用这个术语; 相反,他们只会称它们为闭包或匿名函数,或者发明自己的术语。 在 LISP 中,lambda 只是一个匿名函数。 在Python中,lambda是一个匿名函数,专门限于单个表达式; 除此之外,您还需要一个命名函数。 Lambda 是两种语言中的闭包。

Closures, lambdas, and anonymous functions are not necessarily the same thing.

An anonymous function is any function that doesn't have (or, at least, need) its own name.

A closure is a function that can access variables that were in its lexical scope when it was declared, even after they have fallen out of scope. Anonymous functions do not necessarily have to be closures, but they are in most languages and become rather less useful when they aren't.

A lambda is.. not quite so well defined as far as computer science goes. A lot of languages don't even use the term; instead they will just call them closures or anon functions or invent their own terminology. In LISP, a lambda is just an anonymous function. In Python, a lambda is an anonymous function specifically limited to a single expression; anything more, and you need a named function. Lambdas are closures in both languages.

躲猫猫 2024-07-13 12:51:07

也称为闭包或匿名函数。
我在此处找到了最好的描述。 基本上,内联代码块可以作为参数传递给函数。

Also called closures or anonymous functions..
I found the best description here. Basically, inline block of code that can be passed as an argument to a function.

懷念過去 2024-07-13 12:51:07

“Lambda”指的是 Lambda 演算或特定的 lambda 表达式。 Lambda 演算基本上是处理函数的逻辑和数学的一个分支,并且是函数式编程语言。

〜威廉·莱利·兰德

"Lambda" refers to the Lambda Calculus or to a specific lambda expression. Lambda calculus is basically a branch of logic and mathematics that deals with functions, and is the basis of functional programming languages.

~ William Riley-Land

风柔一江水 2024-07-13 12:51:07

它只是一个内联声明的匿名函数,当您不想编写完整的函数时,通常会分配给委托。

在像 lisp/scheme 这样的语言中,它们通常作为函数参数相当自由地传递,但 C# 中的习惯用法通常发现 lambda 只用于函数的惰性求值,如 linq 中,或者用于使事件处理代码更简洁。

It's just an anonymous function declared inline, most typically assigned to a delegate when you don't want to write a full-fledged function.

In languages like lisp/scheme, they're often passed around quite liberally as function parameters, but the idiom in C# typically finds lambdas used only for lazy evaluation of functions, as in linq, or for making event-handling code a bit terser.

治碍 2024-07-13 12:51:07

编程中并不存在“lambda”这样的东西。 这取决于语言等。

简而言之,通常“具有 lambda”的语言使用该术语来表示匿名函数,或者在某些情况下使用闭包。 就像这样,在 Ruby 中:

f = lambda { return "this is a function with no name" }
puts f.call

There's not really such a thing as 'a lambda' in programming. It depends on the language, etc.

In short, normally a language that 'has lambdas' uses the term for anonymous functions or, in some cases, closures. Like so, in Ruby:

f = lambda { return "this is a function with no name" }
puts f.call
三人与歌 2024-07-13 12:51:07

回应之前的答案:
- 匿名函数的重要之处在于它们不需要名称。
-闭包是一个单独的概念。
- 一篇巨大的维基百科文章并没有让这一点变得更清楚。

这是我的回答,分为三部分:
1. lambda 是一个函数,也是一个表达式。 这是重要的事情。
2. 许多实现所谓“lambda”的语言添加了一些语法糖,以使编写这些短函数更容易、更快,但这不是必需的。
3. 某些语言可能要求 lambda 没有副作用。 从函数意义上来说,这将是一个更纯粹的 lambda。

当函数是表达式时,它是语言中的“一等公民”。 我可以用它做所有重要的事情:

x = lambda(){ return "Hello World"; }

doit( 1, 2, lambda(a,b){ return a > b; }, 3 )

x = (lambda(a){ return a+1; }) + 5  // type error, not syntax error

(lambda(a,b){ print(a); log(b); })( 1, 2 )  // () is valid operator here

In response to the previous answers:
-The important thing about anonymous functions is not that they dont require a name.
-Closures are a separate concept.
-A gigantic wikipedia article is not making this any clearer.

Here's my answer in 3 parts:
1. A lambda is a function which is also an expression. This is the important thing.
2. Many languages which implement so-called "lambdas" add some syntactic sugar to make writing these short functions easier and faster, but this is not required.
3. Some languages may require that a lambda has no side effects. That would be a more pure lambda in the functional sense.

When a function is an expression, it's a "first class citizen" within the language. I can do all the important things with it:

x = lambda(){ return "Hello World"; }

doit( 1, 2, lambda(a,b){ return a > b; }, 3 )

x = (lambda(a){ return a+1; }) + 5  // type error, not syntax error

(lambda(a,b){ print(a); log(b); })( 1, 2 )  // () is valid operator here
同尘 2024-07-13 12:51:07

摘自维基百科:http://en.wikipedia.org/wiki/Lambda#Lambda。 2C_the_word

在 Lisp 和 Python 等编程语言中,lambda 是一个运算符,用于表示匿名函数或闭包,遵循 lambda 演算的用法。

Clipped from wikipedia: http://en.wikipedia.org/wiki/Lambda#Lambda.2C_the_word

In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following lambda calculus usage.

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