58

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?

5
  • 4
    Nitpick - they are called "lambda expressions", not "lambda functions", at least as far as C#/.NET documentation goes.
    – Oded
    Commented Jan 18, 2012 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
    Commented Jan 18, 2012 at 16:51
  • 18
    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
    Commented Jan 18, 2012 at 16:51
  • 4
    Fair enough, here is the link to the stackoverflow answer: stackoverflow.com/questions/16501/what-is-a-lambda-function Commented Jan 18, 2012 at 16:52
  • @ZaphodBeeblebrox: I suspect you're correct about the Half-Life influence. :/ Commented Jan 18, 2012 at 16:59

6 Answers 6

48

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.

3
  • 1
    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. Commented Jan 19, 2012 at 16:32
  • 2
    @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
    Commented Jan 19, 2012 at 16:44
  • 1
    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...) Commented Jan 19, 2012 at 18:51
21

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

7
  • 4
    Note Quite: lambda's can contain state (as in closure does) that they snag from the context where they are declared. Commented Jan 18, 2012 at 20:22
  • 7
    So could a named function, if the language lets you declare nested functions.
    – cHao
    Commented Jan 18, 2012 at 23:49
  • 4
    Kudos for making it into the "low quality posts" review tab with an upvoted answer.
    – yannis
    Commented Jan 19, 2012 at 13:54
  • @ZaphodBeeblebrox - Not intentionally, I can assure you.
    – Oded
    Commented Jan 19, 2012 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... Commented Jan 19, 2012 at 16:30
10

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

Basically Lambda is an anonymous function.

3
  • 2
    So instead of...? Commented Jan 19, 2012 at 0:38
  • Sorry, forgot to remove that part before posting :| Commented Jan 19, 2012 at 8:51
  • Please don't use "basically". It suggests you're dumbing it down.
    – RichieHH
    Commented Mar 28, 2021 at 19:44
8

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);
         }
}

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);
}

// we're passing anonymous function as a third argument
calculate(10, 15, function(x, y) {
    return x + y;
}); // 25
1
  • +1 A lot of people have mentioned that lambdas are anonymous functions, but there is more to it than that. The body (right hand side) of a lambda is often an expression rather than a statement block. The body of a named function being an expression is typically allowed (or required) in functional languages, but not imperative languages.
    – Zantier
    Commented Jul 27, 2016 at 16:17
4

TL;DR As others pointed out: the lambda notation is just a way to define functions without being forced to give them a name.

Long version

I would like to elaborate a bit on this topic because I find it very interesting. Disclaimer: I have taken my course on lambda calculus a long time ago. If someone with better knowledge finds any inaccuracies in my answer, feel free to help me improve it.

Let's start with expressions, e.g. 1 + 2 and x + 2. Literals such as 1 and 2 are called constants because they are bound to specific fixed values.

An identifier such as x is called variable and in order to evaluate it you need to bind it to some value first. So, basically you cannot evaluate x + 1 as long as you do not know what x is.

The lambda notation provides a schema for binding specific input values to variables. A lambda expression can be formed by adding λx . in front of an existing expression, e.g. λx . x + 1. Variable x is said to be free in x + 1 and bound in λx . x + 1

How does this help in evaluating expressions? If you feed a value to the lambda expression, like so

(λx . x + 1) 2

then you can evaluate the whole expression by replacing (binding) all occurrences of the variable x with the value 2:

(λx . x + 1) 2
      2 + 1
      3

So, the lambda notation provides a general mechanism for binding things to variables that appear in an expression / program block. Depending on the context, this creates sightly different concepts in programming languages:

  • In a purely functional language like Haskell, lambda expressions represent functions in the mathematical sense: an input value is injected into the body of the lambda and an output value is produced.
  • In many languages (e.g. JavaScript, Python, Scheme) evaluating the body of a lambda expression can have side-effects. In this case one can use the term procedure to mark the difference wrt pure functions.

Apart from the differences, the lambda notation is about defining formal parameters and binding them to actual parameters.

The next step, is to give a function / procedure a name. In several languages, functions are values like any other, so you can give a function a name as follows:

(define f (lambda (x) (+ x 1)))      ;; Scheme

f = \x -> x + 1                      -- Haskell

val f: (Int => Int) = x => x + 1     // Scala

var f = function(x) { return x + 1 } // JavaScript

f = lambda x: x + 1                  # Python

As Eli Barzilay pointed out, these definition just bind the name f to a value, which happens to be a function. So in this respect, functions, numbers, strings, characters are all values that can be bound to names in the same way:

(define n 42)   ;; Scheme

n = 42          -- Haskell

val n: Int = 42 // Scala

var n = 42      // JavaScript

n = 42          # Python

In these languages you can also bind a function to a name using the more familiar (but equivalent) notation:

(define (f x) (+ x 1))         ;; Scheme

f x = x + 1                    -- Haskell

def f(x: Int): Int = x + 1     // Scala

function f(x) { return x + 1 } // JavaScript

def f(x): return x + 1         # Python

Some languages, e.g. C, only support the latter notation for defining (named) functions.

Closures

Some final observations regarding closures. Consider the expression x + y. This contains two free variables. If you bind x using the lambda notation you get:

\x -> x + y

This is not (yet) a function because it still contains a free variable y. You could make a function out of it by binding y as well:

\x -> \y -> x + y

or

\x y -> x + y

which is just the same as the + function.

But you can bind, say, y in another way (*):

incrementBy y = \x -> x + y

The result of applying function incrementBy to a number is a closure, i.e. a function / procedure whose body contains a free variable (e.g. y) that has been bound to a value from the environment in which the closure was defined.

So incrementBy 5 is the function (closure) that increments numbers by 5.

NOTE (*)

I am cheating a bit here:

incrementBy y = \x -> x + y

is equivalent to

incrementBy = \y -> \x -> x + y

so the binding mechanism is the same. Intuitively, I think of a closure as representing a chunk of a more complex lambda expression. When this representation is created, some of the bindings of the mother expression have already been set and the closure uses them later when it gets evaluated / invoked.

2
  • Im just a begginer, but I think this can be a bit confusing if you are trying to understand λ calculus in the mathematical sense, since what you call constants are called variables and denoted by the symbols a,b,c... What you call variables would be an undetermined variable x. On the other hand 1 is λf x. f x, 2 is λf x. f (f x) and so on.
    – jinawee
    Commented Feb 23, 2019 at 14:54
  • @jinawee: I admit I did not look up the exact definition. I remember using the terms variables and constants in logic. There, a constant is a symbol that is mapped to a domain, whereas a variable is a symbol you can quantify over. But, again, (1) it is a long time ago since I took a course on logic, and (2) lambda-calculus need not follow 1-1 the concepts of mathematical logic. If you point me to a reference I can try and fix my terminology.
    – Giorgio
    Commented Feb 24, 2019 at 15:44
0

"Lambda" in programming usually means "lambda function" (or also "lambda expression", "lambda term"). When function is a named block of code defined before its usage, "lambda function" is a block of code (or an expression) defined in place of the usage that can be used as a first-class citizen in a programming language.

In JavaScript ES6 (2015) there's a short syntax to define lambdas called "Arrow Functions". In C# such syntax was introduced in .NET 3.0 (around 2006).

In math a "function" notion has several meanings where one of the meanings is about a function's notation (i.e. how to write it down), then "lambda function" (in calculus) is a special kind of function notation. For more discussion check lambda functions in programming languages.

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