Context

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.

Looks familiar.

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?

After all, JavaScript doesn’t seem to explicitly use the term. Instead, when learning JavaScript (at least, for me) they were only ever referred to as ‘anonymous functions.’

Akshayhallur, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons

Origin Story

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

By Princeton University, Fair use, https://en.wikipedia.org/w/index.php?curid=6082269

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:

∧x

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.[14] 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,

Russell had the iota operator, Hilbert had the epsilon operator. Why did you choose lambda for your operator?

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.

Usage

In Ruby, everything is an object and lambdas (i.e., anonymous functions) are no exception.

The best way to think of using ‘lambda’ in Ruby, for someone who comes from JavaScript, is to think of the keyword, ‘lambda’, as you would the arrow in arrow syntax (when you utilize arrow syntax for anonymous functions):

In JavaScript:

Technically a named function, use arrow syntax as anonymous easily with just the () => {} syntax.

The only difference being that you’d put the arrow, ‘=>’, to the left of the parenthesis, and replace the arrow with the keyword, ‘lambda.’

In Ruby:

Naming the function here ruby_lambda, you can just as easily use the syntax as an anonymous function: lambda(){}.

About the Arrows

Ruby provides a nice abbreviation for the ‘lambda’ keyword and a pleasant symbol reminiscent of JavaScript’s arrow syntax:

Naming the function here ruby_lambda, you can just as easily use the syntax as an anonymous function: ->(){}.

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.

I know Burns Night isn’t for another month from the writing of this article, but I thought it important to include some sort of Haggis reference.

Also check out these two great articles for a great walkthrough of lambda in Ruby:

Other Resources:

Wikipedia Article for ‘Anonymous Function’

Alonzo Church

StackExchange Posts:

Crazy cool like a mouth full of 花椒 (Sichuan peppers)! Software Engineer | Rails | React | JavaScript | Ruby | AWS S3 | Proud father of a precocious explorer.