3 ms·
> Then there's the confusion between lambdas and closures. They are not the same thing. Lambdas are simply anonymous functions, usually with funny syntax to mak
by saghm 4y ago
> Then there's the confusion between lambdas and closures. They are not the same thing. Lambdas are simply anonymous functions, usually with funny syntax to make them easier to write in line. Closures are functions with data imported from an outer scope. Some languages separate the two.
I remember a few years ago saying that I found the naming of anonymous functions in Rust "closures" rather than "lambdas" weird since most of the time they're used they don't close over anything. Someone responded by saying that all of them closed over something, it just sometimes was unit (the empty tuple type in Rust, essentially the equivalent of "void" when used as a return type). I didn't find it super compelling at the time, although over the years learning more about how much the ownership and borrowing system influenced the way that closures needed to be used in Rust (e.g. having separate traits for functions that close over shared borrows, mutable borrows, and owned values) has made me at least understand why the fact that they close over things is worth emphasizing.
- fulafel 4y agoAlso anonymous functions in programming languages didn't historically always or even usually have lexical scope (= be closures). Eg Lisp had lambda expressions for a long time (20 years? 30 years?) before lexical scoping made it in. Python also had its lambda and nested functions for a long time before lexical scopes came to the language. The namesake, lambda calculus, doesn't have it (I think?).
- tempodox 4y ago> The namesake, lambda calculus, doesn't have it (I think?). Lambdas in the Lambda Calculus have to be closures, how else would you build functions of multiple arguments? Think: λx.λy.(x + y) If they weren't closures, `x` would be unbound in the inner lambda.
- fulafel 4y agoAh, right. And apparently this was a prime motivation to for the first Lispy closures implementation, which was called... Scheme. https://en.wikisource.org/wiki/Scheme:_An_Interpreter_for_Extended_Lambda_Calculus https://en.wikisource.org/wiki/Scheme:_An_Interpreter_for_Ex... "we have implemented an interpreter for a LISP-like language, SCHEME, based on the lambda calculus [Church]"