什么是 lambda 语言?

我当时正在读《 JavaScript: The Good Part 》 ,作者提到 JavaScript 是第一个启动的 lambda 语言。

JavaScript 的函数是具有(大部分)词法范围的第一类对象。JavaScript 是第一个成为主流的 Lambda 语言。在内心深处,JavaScript 与 Lisp 和 Scheme 的共同之处要多于与 Java 的共同之处。它是 C 衣服里的 Lisp。这使得 JavaScript 成为一种非常强大的语言。

我不懂什么是 Lambda 语言。这种语言的属性是什么? 它与 Java、 C、 C + + 和 Php 等语言有什么不同?

29274 次浏览

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.

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

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.

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.

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

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.

  • 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.