5

Now and then I use the Python lambda. Is it so formal that it is safe to say that you can do formal lambda calculus with it? I just used it but I didn't fully understand whether the python lambda and the lambda calculus like I read was done by Alonzo Church. I also used it in Javascript, I think. Isn't this more common in functional languages (e.g. Haskell, Scheme/Lisp, Clojure...) and I never saw lambda in use with Java or C(++)?

Is says on this site " a function you can pass on to another function as argument.".

What is "lambda" code?

But how do I get used to it enough so that I can tell where to benefit from it? If I can do it in say SQL, JPQL or GQL instead, shouldn't I prefer to do it in the query language?

Niklas Rosencrantz
  • 8,008
  • 17
  • 56
  • 95

1 Answers1

6

Python lambda expressions are real, formal untyped λ-calculus lambda expressions.

They fit the formal definition; they can only represent one python expression, based on variables (free or otherwise) and references to other functions (abstract symbols). Python uses parenthesis in expressions too.

You use them wherever a lambda is more suitable and convenient than a full function definition. The python def functionname(argumentlist): syntax forms a statement; in Python you cannot put statements inside of expressions, only the other way around. A lambda on the other hand, is an expression, so you can use a lambda to insert a callback function inline:

map(lambda x, y: x[y+5], [(mapping1, integerkey1), (mapping2, integerkey2)])

The above example consists only of an expression. The python map() function takes, as its first argument, a callable, which is applied to each and every element in the list given by the second argument. In the above example, using a lambda expression to define that callable is much easier than using a function statement:

def mapcallback(x, y):
    return x[y + 5]

map(mapcallback, [(mapping1, integerkey1), (mapping2, integerkey2)])

For the full function syntax I need to assign a name, put the function definition on separate lines, and use the return statement to return the result of the expression.

Martijn Pieters
  • 14,499
  • 10
  • 57
  • 58
  • "Python lambda expressions are real, formal λ-calculus lambda expressions.": I am not an expert of lambda calculus, but I think one should specify if you mean untyped or typed lambda calculus and, if typed lambda calculus is meant, which variation of it (as far as I know, there are more than one). Alternatively, one could add a reference (in the literature) to the formal definition of lambda calculus which is implemented in Python. – Giorgio Feb 05 '13 at 09:52
  • 1
    @Giorgio: Added 'untyped' (python itself is untyped, no formal restrictions are made as to the types you call the lambda with) and a link to the Wikipedia formal definition. – Martijn Pieters Feb 05 '13 at 13:38
  • 1
    Pieter: I would say that Python is dynamically typed rather than untyped. To my knowledge, this is not the same. E.g. in untyped lambda calculus you cannot distinguish between a function, a boolean, and a natural number. – Giorgio Feb 05 '13 at 15:57
  • 2
    @Giorgio: I didn't say Python was untyped; I said that python lambdas are untyped lambdas as described in the WP article: *Another aspect of the untyped lambda calculus is that it does not distinguish between different kinds of data. For instance, it may be desirable to write a function that only operates on numbers. However, in the untyped lambda calculus, there is no way to prevent a function from being applied to truth values, strings, or other non-number objects.* Which is exactly how it works in Python. – Martijn Pieters Feb 05 '13 at 16:02
  • Caveat: I am not an expert neither in lambda calculus nor in Python. What I meant is that in untyped lambda calculus variables in a lambda expression can be bound to values that do not have any type. In Python, variables in a lambda expression can be bound to values of any type, but they do get a type once they are bound. That's why you can execute `(lambda x, y: x + y)(1, 2)` and `(lambda x, y: x + y)("A", "B")`, but `(lambda x, y: x + y)(1, "A")` gives an error. In untyped lambda calculus the last expression would be valid because there is no distinction between truth numbers and strings. – Giorgio Feb 06 '13 at 14:23
  • Maybe the above has nothing to do with the lambda calculus being typed or untyped, I just wanted to better understand this. – Giorgio Feb 06 '13 at 14:25
  • @Giorgio: I am not sure that lamba calculus *defines* what would happen when you mix types like that; not an expert myself either. Generally speaking though, any first-class anonymous function object in a computer language is *some* form of lambda calculus (note the first-class object distinction, it needs to be referenceable using symbols, e.g. storable in a variable). – Martijn Pieters Feb 06 '13 at 14:30
  • My doubts were only regarding the typing: I am quite convinced that Python lambdas correspond to some form of lambda calculus (you can even define a fixed-point combinator). Maybe the typing you have in Python is on another level, i.e. it comes into play when you evaluate built-in functions (like `+`) within a lambda expression. – Giorgio Feb 06 '13 at 14:44
  • This answer is simply wrong. Your own example is a function with 2 arguments, which doesn't exist in lambda calculus. – MaiaVictor Jul 30 '13 at 04:02
  • Python lambdas can take more than one argument, yes. They are a *superset* of lambdas from lambda calculus. The question asked if it was *possible* to do formal lambda calculus with Python lambdas; just because you can use more than one argument doesn't mean they won't work for formal lambda calculus with just one argument. – Martijn Pieters Jul 30 '13 at 09:38