Datasheet

Lambda Calculus (Briefl y)
x
17
As can be surmised from the preceding code, the LINQ Aggregate extension method is the moral
equivalent of the
Reduce written earlier. And as was demonstrated, this means LINQ can be used to
reduce” a collection of Students into an XML representation.
C# 3.0 also offered a slightly more terse way of specifying those delegates to be passed in to the
Database, something called a lambda expression:
Student s = null;
while (s == null)
{
Console.Write(“Please enter your first name:”);
string first = Console.ReadLine();
Console.Write(“\nPlease enter your last name:”);
string last = Console.ReadLine();
s = Students.Find(c => c.FirstName == first &&
c.LastName == last );
if (s == null)
Console.WriteLine(“Sorry! Couldn’t find you”);
}
// Do something with s
The etymology of this name stems quite deeply in computer science history, to a mathematician
named Loronzo Church, who discovered a subtle yet very powerful idea: if mathematical functions
are thought of as things that can be passed around like parameters (just as delegates can), then all
mathematical operations can be reduced to functions taking functions as parameters. And this body
of work came to be known as the lambda calculus, after the Greek symbol that served as the place-
holder symbol for the function name.
LAMBDA CALCULUS (BRIEFLY)
Without getting too deeply into the academic details, Church observed that if the actual function
could be passed around, then various operations that normally seem distinct and individual could be
collapsed together into a single higher-order function.
Consider the following two basic math operations and their C# equivalents:
static int Add(int x, int y) { return x + y; }
static int Mult(int x, int y) { return x * y; }
static void MathExamples()
{
int x = 2;
int y = 3;
int added = x + y;
int multed = x * y;
int addedagain = Add(x, y);
int multedagain = Mult(x, y);
}
What Church realized is that if the actual operation — adding, multiplying, whatever — is abstracted
away as a parameter, then both of these operations can be described using a single function:
delegate int BinaryOp(int lhs, int rhs);
static int Operate(int l, int r, BinaryOp op)
c01.indd 17c01.indd 17 10/1/2010 3:20:38 PM10/1/2010 3:20:38 PM