λ After learning how to program in Haskell, a pure functional programming language, in the edX course offered through TU DelftX I’m convinced more than ever that to do decent functional programming, you need to understand lambda calculus. I’m not saying that you need to master lambda calculus, but you need to understand it insofar as it applies to functional programming and differentiates imperative programming (what we do in sequential, procedural and OOP with PHP, Java, JavaScript, C++, and C#) and functional programming based on lambda calculus. In this post, I’d like to ease into lambda calculus and illustrate how it applies to functional programming in PHP.
It’s Not Like Other Programming Languages
A typical value a programmer may want to generate for a business site is the cost of an item plus shipping charges. By way of example, suppose that the shipping charges are all 11% of the cost of the item. You might write something like the following:
$priceNship = 14.95 + (14.95 * .11);
That’s not especially useful since you need to have a separate set of literals for each item to enter into the variable $priceNship.
You’re most likely to set up a method that handles such calculations with a variable generated through an argument. For example, you might have a class that looks like the following class and method:
class RetailStore
{
private $priceNship;
public function addShipping($x)
{
$this->priceNship = $x + ($x * .11);
return round($this->priceNship,2);
}
}
$worker = new RetailStore();
echo "Cost plus shipping: $" . $worker->addShipping(14.95);
?>
|
I used the $x identifier for the parameter for the addShipping() method instead of something more descriptive like $cost because you’ll often see an x-named variable in lambda calculus.
Casting in Lambda Calculus
When using lambda calculus, one of the key characteristics that I had problems getting used to in Haskell was what you might call stating a problem or abstracting a problem. The problem statement is an abstraction of the problem you want to solve using functional programming. Because lambda calculus strives for abstraction, we’ll start with a simple one:
λx.x + (.11 (x))
What does that mean? First of all λx denotes a lambda function. The x variable is bound to the λ function. (It’s known as a bound variable as opposed to free variables. This post deals with bound variables only.) Put into a PHP class and methods, and setting up a return routine, you have the following:
?php class RetailStoreFunc { public function addShipping($x) { // λx.x + (.11 (x)) $priceNship = function ($x) {return $x + ($x * .11);}; //Round off to 2 decimal points and return return round($priceNship($x),2); } } $worker = new RetailStoreFunc(); echo "Cost plus shipping: $" . $worker->addShipping(14.95); ?> |
The λ function (lambda function) shows that the x value is added to the result of .11 times the value of x. In PHP, a λ function is the same as an anonymous or unnamed function. Now, looking at the PHP version of the λ function, you can see:
λx.x + (.11 (x)) = function ($x) {$x + (.11 *$x);};
If you compare the λ calculus with the PHP function, you can see that while the PHP function is a bit less abstract than the λ calculus expression, they are almost identical. Think of the λ calculus as nothing more than an abstraction of a solution.
If you can understand these introductory elements of λ calculus as applied to PHP, not only are you on the path to understanding λ calculus but also PHP functional programming. It’s not as difficult or large as other calculi; so, rest easy and continue.
Functions within Functions
Not only can λ functions return calculated values, they can return other λ functions. So, if you build one λ function, you can use it in another λ function. In this way you can build functional programs. Further, I have not found any contradiction between λ functions and functional programming and OOP in PHP. This next example shows another example of a λ functions in an OOP context, but unlike the first one this one 1) incorporate and returns one λ function in another, and 2) uses a private variable. You cannot assign a λ function to a variable that is not local (part of the method), but you can assign a non-local variable (e.g., private, public or protected) to store the results of a λ function—just not the λ function itself.
class RetailStoreReady
{
private $final;
public function addShipNtax($x)
{
// λx.x + (.11 (x))
$priceNship = function ($x) {return $x + (.11 * $x);};
// λx.λx + (.07 (x)) (λx. $priceNship(x) + .07 (x))
$totalWithTax = function ($x) use ($priceNship) {return $priceNship($x) + (.07 * $x);};
//Using an embedded property ($final) for storing results
$this->final=round($totalWithTax($x),2);
return $this->final;
}
}
$worker = new RetailStoreReady();
echo "Cost plus shipping and tax: $" . $worker->addShipNtax(14.95);
?>
|
In PHP, employ the use statement when one or more λ functions are within a λ function. To employ more than a single λ function within another λ function, you can separate the λ functions by a comma in the use statement as the following shows:
function ($x) use ($lambdaA, $lambdaB) {return $lambdaA($x) + $lambdaB($x);};
The ability of PHP to incorporate these functional programming elements indicates the commitment the PHP community has to functional programming.
Do You Really Need λ Calculus to learn Functional Programming in PHP?
If you read Simon Holywell’s book Functional Programming in PHP, you will find mention of λ calculus, but only a mention. Indirectly, there are some examples, and I found that some basic understanding of λ calculus is very helpful. But λ calculus is not a requirement for functional programming in PHP or any other language. However, if you do understand something about λ calculus, it is extremely helpful to better grasp functional programming in virtually any programming language. In future posts on functional programming, I will be introducing more elements of λ calculus that pertain to better understanding and using functional programming in PHP.
Recent Comments