It can be easy to see a name or method in a programming language, learn how to use it, and carry one using it without learning anything else about it.
Until my programming education took a turn for the professional and I started learning languages like Python and Ruby, I hadn’t actually encountered lambda except from physics/math classes in college. That is, of course, not to mention a certain video game I enjoyed playing in between those very same classes during college that featured lambda as its exclusive symbol.
My first reaction when first seeing the word, ‘lambda’, in a programming context was like the feeling you get when you see a familiar face but can’t place it.
Lambda, with regards to programming, in as little words as possible is an anonymous function. The main question that stuck in my mind, however, is: why lambda?
The explanation comes when you dig a little deeper with a simple google search for ‘Anonymous Function.’ The result you’ll get is the Wikipedia page for ‘Anonymous Function.’
Staying focused and reading through the article, you’ll find the line where it mentions that the origin of ‘lambda’ in programming comes from its concept in Lambda Calculus. Put succinctly, anonymous functions/lambda are all about ‘function abstraction.’
So, there’s a whole calculus devoted to this. But why is it called lambda?
Digging a little deeper brought me to one of the best places on the internet when you want to know more on just about any topic: Stack Exchange.
The story goes that Alonzo Church was developing lambda calculus as part of a larger question of the foundations of mathematics.
What were these ‘foundations of mathematics’? Recent Software Bootcamp grads will appreciate the next definition I’m about to give, as our lives have become centered on creating algorithms to solve problems on sites like Leetcode and HackerRank. The Wikipedia entry for ‘Foundations of Mathematics’ says it:
“… is the study of the philosophical and logical and/or algorithmic basis of mathematics.”
This line in itself peaks an interest in Lambda Calculus. I feel like even just knowing the name imbues me with “problem solving power.”
The symbol, please
Alonzo Church was writing his paper and needed to differentiate his ‘function abstraction’ from the ‘class abstraction’ used by Alfred North Whitehead and Bertrand Russell in Prinicipia Mathematica.
They used what seems to be a caret, ‘^’, on top of a variable name.
Church put the caret to the left of the letter to indicate function abstraction:
He changed it to an actual lambda, ‘λ’, apparently ‘for ease of printing’ according to the wiki page for Lambda calculus.
It gets better though. Below is my favorite part of this rabbit hole (and probably why I like mathematicians):
Dana Scott has also addressed this controversy in various public lectures. Scott recounts that he once posed a question about the origin of the lambda symbol to Church’s son-in-law John Addison, who then wrote his father-in-law a postcard:
Dear Professor Church,
According to Scott, Church’s entire response consisted of returning the postcard with the following annotation: “eeny, meeny, miny, moe”.
This is one of those things where you take a deep breath and resolve to stop procrastinating more important tasks.
Below is how you use it in Ruby.
In Ruby, everything is an object and lambdas (i.e., anonymous functions) are no exception.
The only difference being that you’d put the arrow, ‘=>’, to the left of the parenthesis, and replace the arrow with the keyword, ‘lambda.’
About the Arrows
For more information on the specifics of lambdas, look at the Ruby documentation for the Proc class, here.
Return of the Lambda
There are a few things to note about lambdas in Ruby, like how the ‘return’ keyword is basically an exit and return back into the enclosing method — different from a normal proc which halts method execution and returns whatever the proc is returning.
Also check out these two great articles for a great walkthrough of lambda in Ruby:
What are Lambdas in Ruby?
Over the last couple of weeks we've looked at working with blocks and Procs. A block is a chunk of code that can be…
Using Lambdas in Ruby
Did you know you can use lambdas as a kind of dynamic hash replacement? The truth is, lambdas are pretty cool once you…
- This paper by Henk Barendregt called “The Impact of the Lambda Calculus in Logic and Computer Science.”