
    Introduction to lambda expressions

Is
     3x + 2y
a function? No, because it does not specify what the inputs are.
(3x+2y is just an expression, it is not a function.)

We need a notation to "declare" which of the variables in the
expression should be input (formal) parameters of the function.

Let us use an "arrow notation" with a function's input parameters
declared on the left of the arrow and the function's expression
given on the right of the arrow.

Here are three different functions built out of the expression 3x+2y.
    x -> 3x + 2y
    y -> 3x + 2y
  x,y -> 3x + 2y
Two of these are functions of one variable (that make use of a
"free variable") and the third is a function of two variables.

In calculus you are used to writing
       y = m*x + b
This represents the function
       x -> m*x + b
Notice that the m and b are assumed NOT to be inputs to the function.
You just know this from habit. But we could use m*x+b to define this
function
     m,x -> m*x+b
or this function
     b,x -> m*x + b
or even this function
   b,m,x -> m*x + b

Compare this to Java/C/C++. Is this a function?
   {
      return 3*x + 2*y;
   }
It's just a block (which could be a function body). It's not
a function because we haven't declared where the variables
are from.

Here are four different functions built out of that one body.
   f1 (int x)
   {
      return 3*x + 2*y;  // y is "non local"
   }

   f2 (int y)
   {
      return 3*x + 2*y;  // x is "non local"
   }

   f3 (int x, int y)
   {
      return 3*x + 2*y;
   }

   f4 (void)
   {
      return 3*x + 2*y;   // both x and y are "non local"
   }

Notice how a Java/C/C++ function definition has (at least) three parts,
   a name,
   a declaration of the input variables,
   a body.

Let's go back to the math notation. These function definitions
only have two parts to them.
    x -> 3x + 2y
    y -> 3x + 2y
  x,y -> 3x + 2y
Each one declares the input parameters and then defines the function
body, but there is no name given to any of these functions. They
are all "anonymous functions".

Our Language_8 has a notation for anonymous functions.
   (lambda  x (+ (* 3 x) (* 2 y)))
   (lambda  y (+ (* 3 x) (* 2 y)))
 (lambda  x y (+ (* 3 x) (* 2 y)))

In Language_8, the following notation will define and immediately call
an anonymous function.

(apply (lambda x y (+ (* 3 x) (* 2 y))) 3 4)

The Haskell programming language has a notation for anonymous functions
that is similar to the math notation.

    \x -> 3*x + 2*y
    \y -> 3*x + 2*y
  \x y -> 3*x + 2*y

In Haskell, the following notation will define and immediately call
an anonymous function.

(\x y -> x*x + y*y) 3 4

Figure out what these expressions are doing.

(\x -> (\y -> x (x y))) (\w -> 3*w) 4

(\x -> (\y -> x*y)) ((\w -> 3*w) 4) 5

(\f g x -> f (g x)) (\x -> 5*x) (\x -> 6*x) 2

(\x -> (\y z -> z (x+y))) 3 4 (\w -> 5*w)


The Language_8 notation for an anonymous function,
   (lambda x (+ (* 3 x x) (* 5 x) 7))
and the Haskell notation for an anonymous function,
   \x -> 3*x*x + 5*x + 7
are often called "lambda expressions". Haskell's "\" character
is kind of an abbreviation of the Greek letter lambda. The name
"lambda expression" comes from a branch of mathematics called
"Lambda Calculus", which was invented in the 1930's.
(https://en.wikipedia.org/wiki/Lambda_calculus)
The Lambda Calculus needed a notation for anonymous functions.
The usual mathematical notation for functions makes you come
up with a name for every function that you define (just like
Java\C\C++). For example, like this
   f(x) = 3*x*x + 5*x + 7
or this
   g(x,y) = x^2 + x*y + y^2
You cannot define an unnamed function using the standard notation
that you learned and used in algebra and (regular) calculus.
(What is wrong with saying that
      x^2 + x*y + y^2
defines an anonymous function?)
Notice that the definition of a function has three main parts,
the name, the list of input parameters, and the function's
expression (body). Lambda Calculus realized that the name part is
not really essential to the definition of a function. A function
exits (just like you do) independently of its name. So Lambda
Calculus created a notation that defined functions without giving
them names. That leaves just the parameter list and the body as
the two parts of a function definition. In Lambda Calculus, a
"lambda expression" is the notation used to define a function.
The letter lambda introduces (starts) the definition of a function.
After the lambda comes the parameter list. The parameter list is
terminated by a period. Then comes the function's body (expression).
Here are a few examples
   \x.3*x*x + 5*x + 7
   \x,y.x^2 + x*y + y^2
   \x.x^2 + x*y + y^2
   \y.x^2 + x*y + y^2
Here, as in Haskell, I'm using "\" as an abbreviation for the Greek
letter lambda. Notice that the period makes for a lousy, unreadable,
notation. So most modern programming languages use an arrow notation,
like -> or =>, to replace the period which separates the function's
parameter list from the function's body. Language_8 (and Lisp and Scheme)
use parentheses to set of the parameter list from the function body.
Lambda Calculus uses commas to separate the parameters in the parameter
list, Haskell and Language_8 (and Lisp and Scheme) just uses spaces.

Lambda expressions have moved from Lambda Calculus to almost all modern
programming languages. They have even recently been introduced to Java
and C++ (but not C).

Here are references for "lambda expressions" in a bunch of common languages.
Java
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax
C++
https://msdn.microsoft.com/en-us/library/dd293608.aspx
JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Python
https://docs.python.org/2/reference/expressions.html#lambda
C#
https://msdn.microsoft.com/en-us/library/bb397687.aspx
Scheme (Racket)
https://docs.racket-lang.org/guide/lambda.html
ML
https://en.wikibooks.org/wiki/Standard_ML_Programming/Expressions#Lambda_expressions
Scala
http://docs.scala-lang.org/tutorials/tour/anonymous-function-syntax.html
Ruby
https://rubymonk.com/learning/books/1-ruby-primer/chapters/34-lambdas-and-blocks-in-ruby/lessons/77-lambdas-in-ruby
Groovy
http://groovy-lang.org/closures.html
Perl
https://perldoc.perl.org/perlsub.html
R
http://adv-r.had.co.nz/Functional-programming.html#anonymous-functions
Matlab
http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
Mathematica
https://reference.wolfram.com/language/tutorial/PureFunctions.html
Maple
http://www.maplesoft.com/applications/view.aspx?SID=1522&view=html&L=F
A lot more.
https://en.wikipedia.org/wiki/Anonymous_function
