Mary Had a Little Lambda
Originally I set out to write a sequel to Functional Programming Fundamentals that covered all of the scary jargon. This quickly evolved into What the Functor? In that article I covered monads, monoids, and functors. Now we have one last bit of scary functional jargon, lambdas.
Originally I set out to write a sequel to Functional Programming Fundamentals that covered all of the scary jargon. This quickly evolved into What the Functor?
In that article I covered monads, monoids, and functors. Now we have one last bit of scary functional jargon, lambdas.
A lambda is an anonymous function that can be treated like a value. 
We actually saw some of these in the previous post. These were
In this case we create an anonymous function and assign it to the variable
joinGryffindor. We then pass this as an argument to
map. We could also just pass this anonymous function directly like so:
This is a common pattern, passing a lambda, or an anonymous function to a higher order function.
Now the scarier term Lambda Calculus. Let's break this down. Lambdas are anonymous functions, we just learned that.
Why don't we define calculus now? If we pull up the dictionary definition we get the following
A method of computation or calculation in a special notation (as of logic or symbolic logic. 
Lambda Calculus is just a formal way of defining functions using special notation. Less scary now, right?
Let's dive just a little more into it. Lambda Calculus, or the formal notation for defining functions, is composed of three elements variables, functions, and applications. 
I'm going to go ahead and heavily paraphrase this great article from learn x in y minutes.
We know these, variables are used in programming all the time, and we can name them anything. Lets go with one named
We know these too! We use functions in our code, and that's why you're reading an article on functional programming. In lambda calculus they use a special syntax:
λ<parameters>.<body>. As an example
λx.x is the identity function. 
This is a new word, but we see this all the time too. This is the act of calling a function. So if a function is the function declaration, this is the actual function call.
The syntax for this even involves parentheses!
(λx.x)a calls the identity function with the argument
Here's a useful table taken from the article mentioned above. 
||a variable named “x”|
||a function with parameter “x” and body “x”|
||calling the function “λx.x” with argument “a”|
This took me a while to grok. If you're looking for more resources I recommend Computerphiles excellent video on Lambda Calculus and the sequel on Y Combinators.
Let's finish up with a comparatively straightforward term. What is a predicate? A predicate is just a function that returns true or false . It's most commonly used as criteria for a filter.
Here's a predicate:
I'd like to reiterate, none of this is important to your day to day code. But if you were curious what all those scary FP terms mean hopefully this satisfied that curiosity.
If you'd like something that will make your code better, check out Functional Programming Fundamentals. If you want even scarier terms check out What the Functor?
Also if you feel the need to buy me a drink you can do so here or follow me on twitter @MatthewGerstman.
These ads help me pay to keep this site up. Feel free to buy, watch, listen or ignore these like any other ad.