By Fred

2008-09-29 18:56:51 8 Comments

Could someone provide a good description of what a Lambda is? We have a tag for them and they're on the secrets of C# question, but I have yet to find a good definition and explanation of what they are in the first place.


@cmeub 2011-08-17 07:42:19

In response to the previous answers:
-The important thing about anonymous functions is not that they dont require a name.
-Closures are a separate concept.
-A gigantic wikipedia article is not making this any clearer.

Here's my answer in 3 parts:
1. A lambda is a function which is also an expression. This is the important thing.
2. Many languages which implement so-called "lambdas" add some syntactic sugar to make writing these short functions easier and faster, but this is not required.
3. Some languages may require that a lambda has no side effects. That would be a more pure lambda in the functional sense.

When a function is an expression, it's a "first class citizen" within the language. I can do all the important things with it:

x = lambda(){ return "Hello World"; }

doit( 1, 2, lambda(a,b){ return a > b; }, 3 )

x = (lambda(a){ return a+1; }) + 5  // type error, not syntax error

(lambda(a,b){ print(a); log(b); })( 1, 2 )  // () is valid operator here

@wprl 2008-09-29 19:00:49

"Lambda" refers to the Lambda Calculus or to a specific lambda expression. Lambda calculus is basically a branch of logic and mathematics that deals with functions, and is the basis of functional programming languages.

~ William Riley-Land

@Eevee 2008-09-29 19:12:54

Closures, lambdas, and anonymous functions are not necessarily the same thing.

An anonymous function is any function that doesn't have (or, at least, need) its own name.

A closure is a function that can access variables that were in its lexical scope when it was declared, even after they have fallen out of scope. Anonymous functions do not necessarily have to be closures, but they are in most languages and become rather less useful when they aren't.

A lambda is.. not quite so well defined as far as computer science goes. A lot of languages don't even use the term; instead they will just call them closures or anon functions or invent their own terminology. In LISP, a lambda is just an anonymous function. In Python, a lambda is an anonymous function specifically limited to a single expression; anything more, and you need a named function. Lambdas are closures in both languages.

@Steven A. Lowe 2008-11-18 04:47:48

the term might be intended to imply lambda calculus

@John Baker 2011-05-08 22:47:44

Wow I with I had more than one upvote to give this question. I've seen so many technical answers bogged down in terminology, but this one is concise and makes the point immediately.

@Daniel 2012-10-08 08:54:14

+1 for not using the term "close over" to define a closure as though it makes it any clearer.

@Eevee 2012-12-03 03:24:50

A region where some variable can exist (i.e., a scope) defined by the structure of your code (i.e., lexically). For example, in most languages, a function defines a lexical scope; any variable declared inside the function is inaccessible outside of it.

@Gulzar Nazim 2008-09-29 19:03:25

Also called closures or anonymous functions.. I found the best description here. Basically, inline block of code that can be passed as an argument to a function.

@JasonTrue 2008-09-29 18:59:49

It's just an anonymous function declared inline, most typically assigned to a delegate when you don't want to write a full-fledged function.

In languages like lisp/scheme, they're often passed around quite liberally as function parameters, but the idiom in C# typically finds lambdas used only for lazy evaluation of functions, as in linq, or for making event-handling code a bit terser.

@rfunduk 2008-09-29 19:04:17

There's not really such a thing as 'a lambda' in programming. It depends on the language, etc.

In short, normally a language that 'has lambdas' uses the term for anonymous functions or, in some cases, closures. Like so, in Ruby:

f = lambda { return "this is a function with no name" }

@Mesh 2008-09-29 18:59:09

Clipped from wikipedia:

In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following lambda calculus usage.

@Fred 2008-09-29 19:03:27

I guess you meant this line: In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following lambda calculus usage. I'd already scanned the article but missed that.

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] What and where are the stack and heap?

23 Answered Questions

[SOLVED] What is a lambda (function)?

41 Answered Questions

37 Answered Questions

[SOLVED] What is dependency injection?

14 Answered Questions

10 Answered Questions

[SOLVED] What is JSONP, and why was it created?

9 Answered Questions

[SOLVED] What is a lambda expression in C++11?

36 Answered Questions

[SOLVED] How can I pair socks from a pile efficiently?

21 Answered Questions

[SOLVED] What is reflection and why is it useful?

14 Answered Questions

[SOLVED] list comprehension vs. lambda + filter

Sponsored Content