Jump to content

Lambda (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jerryobject (talk | contribs) at 09:57, 10 April 2011 (Per WP:MoS: Introductory sentence: article title word moved nearer to start. Headings: links moved to text below. Stub template updated.). 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 Lisp, C#, Ruby and Python,

Examples

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']


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