Dr Darren's links for Lambdas vs Closures for coding languages and formal Lambda Calculus

Icon class
icon_class_computed
fas fa-book

Ever ended up in an argument with a programmer who only uses one language and who thinks that the terminology as used in the one language they know is the definition of a wider concept?

Check out this link for a good description of oft-conflated 'lambdas' vs 'closures' by someone who actually knows the original Lambda Calculus, and the history of lambdas and the various implementations of closures in various languages. The link above is to the 2nd answer on a Stackoverflow forum for: "What is the difference between a 'closure' and a 'lambda'?".

(It's worth reading some of the other answers, although some of them are just plain wrong or are incomplete.)

That recommended answer also describes well how the concept of closure of a function with free (unbound) variables (by provision of the free variables in a context) is often conflated with the particular implementation for carrying that context forward with a "closed" lambda.


Mathematica has lambdas on steroids

Lambdas in the Wolfram Language for Mathematica are covered by the pure anonymous function concept and are just so much easier, the notation is cleaner, and the slot mechanism enables easy handling of multiple arguments (and without all that currying stuff, although one can do that too).

The following is a very simple pure function applied to 4 in the Wolfram Language (the parentheses () are optional but make it a bit clearer):

(3 # ^2 &) [4]

48

And with multiple arguments using the #1, #2 slot notation:

( #1 ^2 + 2 #2 &) [3, 4]

17

And it can be used symbolically, too:

( #1 ^2 + 2 #2 &) [u, v]

u^2 + 2 v

Or indeed "named" (associated with an expression):

f = #1 ^2 + 2 #2 &;
f[u, v]

u^2 + 2 v

Or use a more familiar mathematical mapping notation with named args:

({x, y} |-> x^2 + y^2)[3, 4]

25

Here's an example comparing Wolfram Language and Python:

Fold[#1^2 + 2 #2 &, {2, 3, 5, 7}]

12114
>>> from functools import reduce
>>> out = reduce(lambda x, y: (x**2 + 2 * y) , [2,3,5,7])
>>> 
>>> out
12114

Back to mere coding languages ...

In a minimal lambda calculus one can usually only have one "simple" variable argument, often denoted as 'x'. Extended lambda calculus versions actually permit the argument to be an expression, so the following mapping of a tuple is still a lambda (the point being that there is still only one transformation rule):

(x, y) -> x^2 + y^2

(The above uses a typical mathematical mapping notation, not Church notation.)

One can curry this as:

x -> (y -> x^2 +y^2)

I'm no expert on the history of formal lambda calculus (Church, Frege, Curry etc.) but as it relates to coding languages, the assertion that "lambdas always only take one argument" is not correct. Java, for example, supports this construct with argument tuples:

(arg1, arg2, ...) -> expression
(arg1, arg2, ...) -> { // code block }

Another misconception is that a lambda (in coding languages) can only have a simple "one-line" evaluation expression, which restriction is true for Python, but as you can see above, Java supports lambdas with code blocks.

The moral of the tale is: It's best not to assert that knowledge about one coding language applies to all coding languages, and it's good to learn more than one coding language.

And if you are seeking to engage a "Python specialist" you may in fact be engaging someone who has ONLY ever used Python, and you are probably better off with an experienced software engineer who understands high level principles that apply to all coding languages. And knows some Python.


"I'm using lambdas everywhere in Python because it supports lambdas and therefore that's the Pythonic way"

Read Overusing lambda expressions in Python by Trey Hunner (and note especially his tip about the operator library). Overuse of lambdas also risks creating too much WET code, because you are perhaps overlooking the opportunity to capture a reusable strategy as a named function, one that you can also document well (which you can't easily do with lambdas), and inject into functions such as reduce and accumulate anyway.

If an operation is so simple that it "only warrants" a lambda there might be a named core or library function for it already; if it is not so simple, it might warrant having a reusable, documented, function.


The External Links section below also has some nice references and tutorials on formal Lambda Calculus, use of lambdas in Python, lambdas and closures in C++ (which in C++ are always associated with lambdas), and more.

Notes
Relevant snippets (from other sources)
Visit also
Visit also (backlinks)
External links
Flags