什么是 lambda 语言?

发布于 2024-09-26 01:12:48 字数 354 浏览 7 评论 0原文

我正在阅读《JavaScript:The Good Parts》,作者提到 JavaScript 是第一个推出的 lambda 语言。

JavaScript 的函数是具有(大部分)词法作用域的一流对象。 JavaScript 是第一个成为主流的 lambda 语言。从本质上讲,JavaScript 与 Lisp 和 Scheme 的共同点比与 Java 的共同点更多。它是披着 C 外衣的 Lisp。这使得 JavaScript 成为一种非常强大的语言。

我不明白什么是 lambda 语言。这种语言有什么特性?它与 Java、C、C++ 和 Php 等语言有何不同?

I was reading "JavaScript: The Good Parts" and the author mentions that JavaScript is the first of the lambda languages to be launched.

JavaScript's functions are first class objects with (mostly) lexical scoping. JavaScript is the first lambda language to go mainstream. Deep down, JavaScript has more in common with Lisp and Scheme than with Java. It is Lisp in C's clothing. This makes JavaScript is remarkably powerful language.

I didn't get what is a lambda language. What are the properties of such a language and how is it different from languages like Java, C, C++ and Php?

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

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

发布评论

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

评论(7

芸娘子的小脾气 2024-10-03 01:12:49
  • JavaScript 允许定义匿名函数,即不绑定到标识符的函数。这样的函数也称为 Lambda 抽象,并且由于 JS 支持此功能,因此被称为 Lambda 语言。

  • 属性:在立即执行函数或短期使用的情况下需要此函数,其中为函数命名没有意义。

  • 它与 Java、C、C++ 和 PHP 等语言不同,因为 JS 中的匿名函数用于闭包和柯里化。

  • JavaScript allows to define Anonymous function that is a function which is not bound to an identifier. Such function is also known as Lambda Abstraction and since JS support this it is known as Lambda Language.

  • Properties : This function are needed in case of immediate execution of a function or for short term use, where there is no significance of giving name to function.

  • It is different from languages like Java, C, C++ and PHP as in JS Anonymous functions are used for Closure and Currying.

情仇皆在手 2024-10-03 01:12:48

简单来说,lambda 语言是一种允许将一个函数传递给另一个函数的语言,其中该函数被视为任何其他变量。此外,您应该能够定义此函数以匿名(或内联)方式传递。 PHP 5.3 添加了对 lambda 函数的支持。 JavaScript 是第一个主流语言吗?在 JavaScript 之前,Lisp 已广泛应用于教育环境,也用于定制我们喜爱的 Emacs http ://www.gnu.org/software/emacs/manual/html_node/eintr/

这是一个例子

function applyOperation(a, b, operation) {
  return operation(a,b);
}

function add(a,b) { return a+ b; }
function subtract(a,b) {return a - b;}

// Can be called like
applyOperation(1,2, add);
applyOperation(4,5, subtract);
// Anonymous inline function
applyOperation(4,7, function(a,b) {return a * b})

它与 C 有什么不同?在 C 中,您可以将指针传递给函数,但不能匿名内联定义它。

在Java(版本8之前)中,要达到相同的效果,必须传递一个实现接口的对象,该对象实际上可以匿名内联定义。

A lambda language, in simple terms, is a language that allows passing a function to another function, where the function is treated as any other variable. Also, you should be able to define this function to be passed anonymously (or inline). PHP 5.3 added support for lambda functions. Was JavaScript the first mainstream language? Lisp has been widely used in educational settings before JavaScript and also in customizing our beloved Emacs http://www.gnu.org/software/emacs/manual/html_node/eintr/

Here's an example

function applyOperation(a, b, operation) {
  return operation(a,b);
}

function add(a,b) { return a+ b; }
function subtract(a,b) {return a - b;}

// Can be called like
applyOperation(1,2, add);
applyOperation(4,5, subtract);
// Anonymous inline function
applyOperation(4,7, function(a,b) {return a * b})

How is it different from C? In C, you can pass pointer to functions, but you can't define it inline anonymously.

In Java (before version 8), to achieve the same effect, you must pass an object that implements an interface, which actually can be defined anonymously inline.

奶茶白久 2024-10-03 01:12:48

我从未听说过有人使用“lambda 语言”这个术语,而且我能想到的唯一合理的定义将 JavaScript 排除在“第一个”之外。

也就是说,我怀疑他的意思可能是:

  • 函数式语言:一类语言,其中计算被(或可以)建模为(可能是高阶)函数的无状态组合。 LISP、Scheme、ML、Haskell 等经常被归为此类,尽管其中一些是更恰当的混合范式或“函数式可选”语言。 Javascript 可以说包含了使“函数式”编程成为可能的必要功能。
  • 允许创建匿名函数的语言(在 JavaScript 中使用 function 语法;在许多语言中都写成 lambda,因此可能是“lambda 语言”。

这两种用法都派生自使用希腊字母 lambda 来表示 lambda 演算中的函数抽象,这是 Alonzo Church 设计的计算模型,也是函数式编程的基础

。 ,这是有争议的,我认为 LISP 至少在某种程度上是主流,但这是一个公平的观点,JavaScript 的语义直接受到Scheme 的启发,并且它肯定比任何其他可以提出类似主张的语言拥有更多的受众。

I've never heard anyone use the term "lambda language," and the only plausible definitions I can think of would exclude JavaScript as "the first."

That said, I suspect he may mean either:

  • Functional languages: a class of languages in which computation is (or can be) modeled as a stateless composition of (possibly higher-order) functions. LISP, Scheme, ML, Haskell, etc. are frequently ascribed to this class, although several of these are more properly mixed paradigm or "functional optional" languages. Javascript arguably contains the necessary features to make a "functional style" of programming possible.
  • Languages which allow the creation of anonymous functions (using the function syntax in JavaScript; this is written lambda in many languages, hence possibly "lambda languages."

Both usages are derived from the use of the greek letter lambda to denote function abstraction in the lambda calculus, the model of computation devised by Alonzo Church and upon which functional programming is based.

Edit: looked at Google Books result---"first to go mainstream"; well, that's arguable. I'd put forward that LISP was at one point at least reasonably mainstream. It's a fair point though, JavaScript's semantics are directly inspired by Scheme and it certainly reached a larger audience than any other language that can make similar claims.

不即不离 2024-10-03 01:12:48

他指的是Lambda 演算

Lambda演算,也写作λ-演算,是函数定义、函数应用和递归的形式化系统。
[...]

[...]无类型 lambda 演算是函数式编程(尤其是 Lisp)的最初灵感,而类型化 lambda 演算则成为现代类型系统的基础。

He refers to Lambda calculus.

Lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion.
[...]

[...] with untyped lambda calculus being the original inspiration for functional programming, in particular Lisp, and typed lambda calculi serving as the foundation for modern type systems.

他夏了夏天 2024-10-03 01:12:48

我见过 lambda 被定义为匿名函数和对函数的引用。 Javascript 两者都支持:

setTimeout(function(){ /* an anonymous function */ }, 100)

var f = function(){ /* function ref */ }

这就是 JS 获得强大功能和灵活性的地方。 Java 在某种程度上支持第一个(匿名接口实现),但不支持后者 - 请参阅下面的 Java 8 更新。

我不清楚其中哪一个(或两者)是 a 的正确定义拉姆达。

JS 绝对不是第一个支持这些功能的语言。根据记忆,我认为语言爱好者总是热衷于支持 lambda,这只是闲聊。

顺便说一句:在 Java 中,匿名类通常用于动态传递类定义作为参数(在 swing 中经常使用)。像这样的东西(凭记忆,未编译):

someGuiContainer(new WidgetInterface()
      {
           public void importantMethodToDefine(){
             // Handle having the method called in my special widget way
           }
        }
)

更新

Java,从 8 开始,现在正式成为 Lambda 语言。

您现在可以使用以下语法:

MathOperation addition = (int a, int b) -> a + b;
System.out.println("10 + 5 = " + tester.operate(10, 5, addition));

代码源

I've seen a lambda defined as an anonymous function and as a reference to a function. Javascript supports both:

setTimeout(function(){ /* an anonymous function */ }, 100)

var f = function(){ /* function ref */ }

This is where JS gets a lot of its power and flexibility. Java supports the first to some extent (anonymous interface implementations), but not the latter - see below for update for Java 8.

Its unclear to me which (or both) of these is the proper definition of a lambda.

JS is definitely not the first language to support these features. Going from memory, I think its smalltalk that language enthusiasts always rave about supporting lambdas.

BTW: In Java, an anonymous class is usually used to pass in a class definition on the fly for an argument (used a lot in swing). Something like this (from memory, not compiled):

someGuiContainer(new WidgetInterface()
      {
           public void importantMethodToDefine(){
             // Handle having the method called in my special widget way
           }
        }
)

Update

Java, as of 8, is now officially a Lambda language.

You can now use the following syntax:

MathOperation addition = (int a, int b) -> a + b;
System.out.println("10 + 5 = " + tester.operate(10, 5, addition));

Code Source

暖风昔人 2024-10-03 01:12:48

在麻省理工学院的开放课件《计算机程序的结构和解释》中,有哈尔·阿贝尔森 (Hal Abelson)、杰里·萨斯曼 (Jerry Sussman) 和朱莉·萨斯曼 (Julie Sussman) 合着的书。他们讨论了Scheme,它是LISP 的一种方言,并且他们对什么是lambda 以及Scheme LISP 和一般语言进行了非常详细和清晰的解释。如果您希望对计算机编程有一个真正清晰和深入的了解,我强烈建议您看看它。向你解释会花费三倍的时间,如果你去那里只是阅读书籍或观看完美解释它的教程,这真是天才。

Javascript主要基于Scheme语言及其Lisp之父,此外它还采用了lamda结构并随之成为主流。

In MIT's open course-ware called structure and interpretation of computer programs a book by Hal Abelson's, Jerry Sussman's and Julie Sussman's. They discuss Scheme, which is a dialect of LISP and there they explain a very detailed and clear explanation of what lambda is and Scheme LISP and languages in general. I highly recommend you look at it if you wish to have a really clear and deep understanding of Computer Programming. To explain to you would take three times as much time as if you went there and just read the book or watch the tutorials which explains it perfectly, it's genius.

Javascript is mainly based off of the language Scheme and it's Lisp father, and in addition it took its lamda structure and went mainstream with it.

茶花眉 2024-10-03 01:12:48

来自维基百科:
在 Lisp 和 Python 等编程语言中,lambda 是一个运算符,用于表示匿名函数或闭包,遵循 lambda 演算的用法。在 Python 语言中使用 lambda 的一个示例是这部分计算机代码,它按每个条目的最后一个字符按字母顺序对列表进行排序:

>>> list = ['woman', 'man', 'horse', 'boat', 'plane', 'dog']
>>> sorted(list, key=lambda word: word[-1])
['horse', 'plane', 'dog', 'woman', 'man', 'boat']

* In the C# programming language a lambda expression is an anonymous function that can contain expressions and statements

From wikipedia:
In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus. An example of this use of lambda in the Python language is this section of computer code that sorts a list alphabetically by the last character of each entry:

>>> list = ['woman', 'man', 'horse', 'boat', 'plane', 'dog']
>>> sorted(list, key=lambda word: word[-1])
['horse', 'plane', 'dog', 'woman', 'man', 'boat']

* In the C# programming language a lambda expression is an anonymous function that can contain expressions and statements
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文