Datasheet
Lambda Calculus (Briefl y)
x
19
get { return m_majorApproval; }
}
}
class Class
{
// ...
public bool Assign(Student s)
{
if (s.CanTake(this))
{
Students.Add(s);
return true;
}
return false;
}
}
Now, the validation code can be kept in a third place, thus localizing it to neither the Student nor
the
Class, but someplace accessible to either, such as a ProgramSignup collection someplace that
the
Student constructor can access (or the Class.Assign method could consult directly, depending
on the developer’s sense of aesthetics):
ProgramValidation[“ComputerScience”] =
c => c.Field == “Computer Science”;
ProgramValidation[“Physics”] =
c => c.Field == “Computer Science” ||
c.Field == “Physics”;
ProgramValidation[“Psychology”] =
c => c.Field == “Psychology” ||
c.Field == “Grade school”;
ProgramValidation[“Grade school”] =
c => false;
ProgramValidation[“Video game design”] =
c => c.Field != “Underwater Basketweaving”;
It should be noted that yes, something similar to this could be modeled in a traditional object-ori-
ented way, usually by modeling the rules as function objects (like the Strategy approach earlier), also
sometimes referred to as functors, and has been around for quite a while; C and C++ used pointers-
to-functions to do things like this for decades.
The lambda calculus also permits more than just “lifting” the operation out and capturing it,
though. As originally expressed, the lambda calculus also states that if a function can accept a func-
tion as a parameter, then all functions are essentially functions of one parameter, even though they
may appear otherwise.
For example, returning to the basic mathematic operation of addition, the basic C# method of
earlier:
static int Add(int x, int y) { return x+y; }
static void MoreMathExamples()
{
int result = Add(2, 3);
}
c01.indd 19c01.indd 19 10/1/2010 3:20:38 PM10/1/2010 3:20:38 PM