3 ms·
I'll try to tackle the first two :-) A lambda is an anonymous function; it's a function that doesn't have a name. Of course you can assign it to a variable if
by hencq 15y ago
I'll try to tackle the first two :-)
A lambda is an anonymous function; it's a function that doesn't have a name. Of course you can assign it to a variable if you like to. So for example in Javascript these two are equivalent:
function foo(arg) {//function body}
foo = function(arg) {//function body}
Or in Python
def foo():
return 42
foo = lambda: 42
Note that functions (and lambdas) are 'first class citizens'. This means we can pass functions around like any other type of value. You could sort of compare this to function pointers.
Now a closure is related to these first class functions. It's basically a function that carries with it, the environment from when it was defined. Consider this Python/pseudocode:
def foo():
a = 42
def bar():
return a
return bar
myClosure = foo()
myClosure() #this returns 42
The function foo returns a function that we assign to myClosure (in the function foo we named it bar, but we could have used an anonymous lambda as well). The returned function returns the value of a, but note that a was only around when we defined bar. When we call myClosure, it takes the value for a from the environment when it was defined.
So, lambdas and closures are not the same thing, but they're both related to first class functions. In practice when a language supports first class functions, it tends to support lambdas and closures as well.
Monads are something different again. They're used in purely functional languages (such as Haskell) to work with state. However, that's about as far as my knowledge goes so someone else can try explain that :-)