Lambda (programming)
Encyclopedia
Lambda is an operator used to denote anonymous function
Anonymous function
In programming language theory, an anonymous function is a function defined, and possibly called, without being bound to an identifier. Anonymous functions are convenient to pass as an argument to a higher-order function and are ubiquitous in languages with first-class functions such as Haskell...

s or closures
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...

, following the usage of lambda calculus
Lambda calculus
In mathematical logic and computer science, lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion. The portion of lambda calculus relevant to computation is now called the untyped lambda calculus...

, in programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s such as C#, Erlang, Lisp, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, Scala, and recently C++.

C#

In C#, lambda expressions are often used with LINQ
LINQ
Linq is a word-based card game from Endless Games, introduced at the American International Toy Fair in 2005.Game play requires at least four players, two of whom are dealt cards with the same word, while the others receive blanks. The goal is to gain points by correctly naming the players with...

:

var allWikipediaPages = GetAllWikipediaPages;
var lambdaWikipediaPage = allWikipediaPages.First(wp => wp.Title "Lambda (programming)");

C++

In C++, lambda expressions can take this form:

auto square = [] (int x) -> int { return x * x; };
square(5); // returns 25

Erlang

In Erlang, lambda expressions (usually called as "funs") can take this form:

F = fun(X) -> X * X end,
F(5). % returns 25

Haskell

In Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

, lambda expressions can take this form:

Prelude> let f = \x -> x + 1
Prelude> :t f
f :: Integer -> Integer
Prelude> f 2
3

Python

In Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, an example of this use of lambda is this sample of computer code that sorts a list alphabet
Alphabet
An alphabet is a standard set of letters—basic written symbols or graphemes—each of which represents a phoneme in a spoken language, either as it exists now or as it was in the past. There are other systems, such as logographies, in which each character represents a word, morpheme, or semantic...

ically by the last character of each entry:

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

Scala

In Scala, lambda expressions can take this form:

scala> (x:Int, y:Int) => x + y
res0: (Int, Int) => Int =
scala> res0(1, 2)
res1: Int = 3

Argument types can be inferred when applied to a list:

scala> List(1, 2, 3, 4)
res0: List[Int] = List(1, 2, 3, 4)
scala> res0.foldLeft(0)((x, y) => x + y)
res1: Int = 10
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK