Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a little bit confused about 'function' and 'lambda'. I've seen some examples showing that the scheme keyword lambda works very similarly to the JavaScript keyword function, but I really don't know how they are related.

I'm told that 'function' and 'method' can be used interchangeably when speaking about objects in .net. I'm wondering if 'lambda' and 'function' similarly mean the same thing. I wonder if 'lambda' has some esoteric meaning, seeing that the Greek letter lambda (λ) appears in so many avatars on this site. To make things even more confusing, in .net, the functional parts of C# refer to function expressions passed to another function as 'lambda expressions', so the word really seems to be all over the place.

I'm also vaguely familiar with the term 'lambda calculus'.

What is the difference between a function and a lambda?

share|improve this question
2  
Nitpick - they are called "lambda expressions", not "lambda functions", at least as far as C#/.NET documentation goes. –  Oded Jan 18 '12 at 16:39
    
@TWith2Sugars - Read the message. Your answer is low quality as it pretty much is just a link, so got converted to a comment. –  Oded Jan 18 '12 at 16:51
13  
I wonder if 'lambda' has some esoteric meaning, seeing that the Greek letter lambda (λ) appears in so many avatars on this site. One would hope it would be in reference to lambda calculus, but I have a strange feeling Half Life is to blame for lambda avatars. –  Yannis Rizos Jan 18 '12 at 16:51
3  
Fair enough, here is the link to the stackoverflow answer: stackoverflow.com/questions/16501/what-is-a-lambda-function –  TWith2Sugars Jan 18 '12 at 16:52
    
@ZaphodBeeblebrox: I suspect you're correct about the Half-Life influence. :/ –  FrustratedWithFormsDesigner Jan 18 '12 at 16:59

5 Answers 5

Clearly the last example of an anonymous function in javascript is not anonymous as its name is "add". I think of lambda expressions as the half-way point between very simple procedural programming and object orientation. Simply you can pass values or pointers -- lambda lets you pass procedure (code blocks) -- objects are the full encapsulation of values and methods. C allows you to pass function pointers. And of course java, javascript and c# handle completely anonymous (having no name) functions.

The add function above is lambda expressed but is certainly not anonymous.

share|improve this answer
    
You shouldn't be posting blind responses to other answers, it makes it kinda difficult to follow, and is bad form for this site. Please see the FAQ for a better description. –  Jeff Langemeier May 7 '13 at 15:47

Answered Here: http://stackoverflow.com/questions/16501/what-is-a-lambda-function

Basically Lambda is an anonymous function.

share|improve this answer
1  
So instead of...? –  Bryan Oakley Jan 19 '12 at 0:38
    
Sorry, forgot to remove that part before posting :| –  TWith2Sugars Jan 19 '12 at 8:51

In C# Anonymous function is a general term that includes both lambda expressions and anonymous methods (anonymous methods are delegate instances with no actual method declaration).

Lambda expressions can be broken down to expression lambda and statement lambda

Expression lambda:

(int x, string y) => x == y.Length 

Statement lambda is similar to expression lambda except the statement(s) are enclosed in braces:

(int x, string y) => {
         if (x == y.Length) {
             Console.WriteLine(y);
         }
}

In JavaScript the anonymous method is declared like this:

var add = function(x, y){
    return x + y;
};

When we talk about lambda expressions in JavaScript that basically just means using a function as an argument in a call to another function.

var calculate = function(x, y, operation){
    return operation(x, y);
}

calculate(10, 15, add); // 25
share|improve this answer

The word "lambda" or "lambda expressions" most often refers to anonymous functions. So in that sense a lambda is a kind of function, but not every function is a lambda (i.e. named functions aren't usually referred to as lambdas). Depending on the language, anonymous functions are often implemented differently than named functions (particularly in languages where anonymous functions are closures and named functions are not), so referring to them with different terms can make sense.

The difference between scheme's lambda keyword and Javascript's function keyword is that the latter can be used to create both anonymous functions and named functions while the former only creates anonymous functions (and you'd use define to create named functions).

The lambda calculus is a minimal programming language/mathematical model of computation, which uses functions as its only "data structure". In the lamdba calculus the lambda-symbol is used to create (anonymous) functions. This is where the usage of the term "lambda" in other languages comes from.

share|improve this answer
    
That is extremely rough. You use define (or let or one of its relatives, or an internal define) to create names -- that's all. There's nothing special in define with respect to functions. –  Eli Barzilay Jan 19 '12 at 16:32
1  
@EliBarzilay Well, define does have a special form for defining functions (i.e. you can write (define (f x) (foo)) instead of (define f (lambda (x) (foo)))), but my point was that you can't create a named function using lambda alone, i.e. you can't write something like (lambda f (x) (foo)) to define a function named f that takes one argument like you can with Javascript's function keyword. –  sepp2k Jan 19 '12 at 16:44
    
define has that as a syntactic sugar, so it's not as important as its role as a name binding tool for all values. As for lambda not creating a name by itself: that's an important feature, since it separates name giving from function forms... IMO JS is doing the right thing in allowing the separation while also accepting an optional name for those masses who would be horrified at the idea of a function with no name. (And luckily the size of those masses are in a general decline...) –  Eli Barzilay Jan 19 '12 at 18:51

A lambda is simply an anonymous function - a function with no name.

share|improve this answer
3  
Note Quite: lambda's can contain state (as in closure does) that they snag from the context where they are declared. –  Loki Astari Jan 18 '12 at 20:22
4  
So could a named function, if the language lets you declare nested functions. –  cHao Jan 18 '12 at 23:49
2  
Kudos for making it into the "low quality posts" review tab with an upvoted answer. –  Yannis Rizos Jan 19 '12 at 13:54
    
@ZaphodBeeblebrox - Not intentionally, I can assure you. –  Oded Jan 19 '12 at 13:59
    
Not really, a lambda expression in Scheme is just like a function expression with no name -- but there's nothing stopping you from later giving them a name. For example var f = [function(x){return x;}][0]. You could argue that the function value itself has no name, but that would be true for all functions... –  Eli Barzilay Jan 19 '12 at 16:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.