Jump to content

Lambda (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tea2min (talk | contribs) at 07:19, 13 April 2011 (Fix markup, add to Category:Programming constructs). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus, in programming languages such as C#, Lisp, Python, and Ruby.

Examples

C#

In C#, lambda expressions are often used with LINQ:

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

Haskell

In Haskell, 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, an example of this use of lambda is this sample of computer code that sorts a list alphabetically 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']