First of all, what is "Lambda Calculus"?
You could try looking it up on Wikipedia, but that may be a little wordy, so I'll explain it right here.
TL;DR: (λstuff.(stuff and more stuff))(Things) -> (Things and more Things)
Basics: Substitution
Lambda Calculus is 99% substitution and 1% expressions. The 1% comes from what's left in the end.
Have you ever written a printf() command in C? It can go like this:
Code: Select all
printf("Hello %s", "there");Code: Select all
Hello thereThat's basically how Lambda Calculus works. It takes an argument, and replaces something inside its body.
If we were to write the above in Lambda Calculus notation, it'd be this:
Code: Select all
(λx.(Hello x))(there)Code: Select all
Hello (there)Instead of "Hello %s" we have (Hello x)
This says it's a function (a.k.a. "Lambda" or "Expression") that takes in a variable "x" and returns the expression "Hello (x)".
Notice how I said it returns an "Expression" and not a "String".
In reality, there are no strings in Lambda calculus. The above example would treat each letter of "Hello" as the variables 'H', 'e', 2 of 'l', and 'o'.
This means that, if we put that in something that takes 'l' as an argument, we could do this:
Code: Select all
(λl.(Hello (there)))(VAR)Code: Select all
He(VAR)(VAR)o (there)Code: Select all
(λz.z z)(Hello)
(Hello) (Hello)
Code: Select all
(λy.Period y)(15)
Attempt (15)
Now, the quirk of Lambda Calculus is that you can make an expression return another valid expression.
To explain this, I'll just give an example.
Code: Select all
(λx.(λy.(I like the colors x and y))(blue))(red)Code: Select all
(λx.
(λy.(I like the colors <x> and y))(blue)
)(red)
For now, let's just ignore everything other than that <x> that we're substituting.
Code: Select all
(λx.
(λy.(I like the colors x and y))(blue)
)(red)
take x = (red)
(λy.(I like the colors <x> and y))(blue)
substitute <x> with (red)
(λy.(I like the colors (red) and y))(blue)
But wait, we can still evaluate that, so let's keep going.
Code: Select all
(λy.
(I like the colors (red) and y)
)(blue)
take y = (blue)
(I like the colors (red) and <y>)
substitute <y> with (blue)
I like the colors (red) and (blue)
Recursion
Now, watch:
Code: Select all
(λx.x x)(λx.x x)
Code: Select all
(λx.x x)(λx.x x)
Take x = (λx.x x)
<x> <x>
substitute all x's with (λx.x x)
(λx.x x)(λx.x x)
This works with other expressions, and you can also give them your own arguments as well.
Here's an expression, that takes another expression, and gives it 15.
Code: Select all
(λx.x 15)(λy. Period y)
x = (λy. Period y)
<x> 15
(λy. Period y) 15
y = 15
Period <y>
Period 15