What is it used for? Examples ...
- Here you can write about almost everything - Niki-Timofe
- five@ niki-timofe, this is a weak excuse. If you don’t make an effort to ask a good question, you can’t expect us to make an effort to give a good answer. Thus, you will have to be satisfied with the copy-paste from the link above, which is somewhat silly for the programmer, since it implies manual work to be done on the machine. - karmadro4
4 answers
Lambda functions are functions that actually have no name. Thus, mathematicians simplified to the impossibility the format of the recording function, and in general lambda calculi tried to formalize the calculations
λx.x
λ
- means that it is a lambda function. All that is after it is a list of arguments, ideally of absolutely any type, including another lambda function. After the point comes the "function body", and only then, in fact, the argument goes, which will be passed. So
λx.x+2 2 // вернёт 4
The example is more complicated:
λx.x 2 λy.y+1 // результат 3
Here, the x parameter is another lambda function λy.y + 1
, into which parameter 2 is passed. That is, any lambda function is a higher order function, it can take another function as an argument and return a function:
λx.λy.y+x+3 2 // вернёт λy.y+5, т.к. x был равен двум. λx.λy.y+x+3 2 3 // вернёт 8. Фактически это каррирование: сначала функция принимает аргумент 2 и возвращает функцию, которая принимает ещё один аргумент и возвращает результат.
If interested, I once wrote similar things in C #
Now let's see how all our examples will look like in C #. Here, as the lambda function, I use Func , where T is the type of the argument, and U is the type of the return value:
1) Func<int, int> func = x=>x; 2) var result = new Func<int,int>(x=>x+2)(2); 3) var result = new Func<Func<int,int>, int>(x=>x(2))(new Func<int,int>(y=>y+1)); 4) var result = new Func<int, Func<int, int>>(x=>y=>y+x+3)(2); 5) var result = new Func<int, Func<int, int>>(x=>y=>y+x+3)(2)(3);
The difficulty is only an explicit indication of the type of the arguments and the return value.
λx.λy.y+x+3 2
is called our currying (currying, partial application) in honor of Haskell Curry. (see programming languages haskell and curry) - alexlz- 2What are you talking about? besides, currying and partial use are different things - Specter
- Then I would like to see the official translation of the term currying. (Carring did not seem to be established in Russian yet) - alexlz
- I know that Wikipedia is not the most reliable source, but still - http://ru.wikipedia.org/wiki/Carry - Specter
- 2@Andrew. In order to understand the differences, you need to delve into C #. This bother, if I understood correctly, occurs when a special syntax is used to call a function. Whereas in the case of lambda calculus such thin plates are not found due to their uselessness. - alexlz
Lambda expressions are anonymous functions. Crawled out of mathematics, where a special form of function recording was used, eliminating the function / value function ambiguity, etc. Thanks to the efforts of the designers (not html-, but typographic sets) from the form $ hat xx $, it was transformed into $ wedge xx $, well , already naturally - in $ lambda xx $
Those. The value of a lambda expression is a function that can be applied to some argument / arguments.
- Who else would have explained how to write the formulas here ... And then something ran through the LaTeX format. - alexlz
- Alas, it does not work here, only on Math. - karmadro4
- Then with words. Initially, x "under the header ^". X, was converted into a "conjunction sign" xx, and only then lambda xx - alexlz
lambda is in simple terms a function that lies directly in a variable, if we want to write a function into a variable, then we must first describe this function
def main(a,b): return a+1,b-1 tools = main(3,4) // 43 print(tools)
using lambda
a,b = (3,4) tools = lambda a,b: (a+1, b-1) print(tools)
thanks to writing in one line, it can be conveniently inserted anywhere.
- 3As for python, there lambda expressions are weak (if they are not changed in python3), they do not allow to describe complex functions. The argument is that in python it is easy to create local functions, and this allows you not to grieve about the names. Lambda in javascript (function (x) {return x + 1}) (5); var f = function (x) {return x * 2}; - alexlz
Specify the details (language in particular). Lambda expressions are anonymous functors (variables containing an entire function). The lambda expression allows closure to the lexical context in which this expression is used. In PHP, for example, such an expression is created like this:
$func = create_function("$a,$b","return $a+$b;"); // lambda-style функция echo $func(1,2); //3
- fourIn PHP,
create_function
has no relation to anonymous functions and is a wrapper foreval
, which for each call creates a named function with the name "lambda_N
", where N is a positive integer. The relatively new syntax$f = function(...) { ... }
already behaves like an anonymous function (for example, is not overdeclared in a loop), and is an object of typeClosure
that behaves like a function (but is not a function, as a result, it does not work everywhere). - drdaeman - 2The question is nowhere: Why is there not a single word on php.net about it? .. (I’m talking about eval'e) I suppose this is a "little bit" important information that should be in bold in the description of the function (or better before all) , but no: create_function - Create an anonymous (lambda-style) function - Zowie
- 2In pohapa added, because go to Kuznetsky, all in lacquer . But the ending is predictable - it turned out as always. - karmadro4
- fourBe careful using the term "functor". In different languages (programming), it has a different meaning. Given in Wikipedia - from the Prolog language. Here is the definition from another article in the same pediviki: "Functors are categories mappings that preserve structure." - alexlz
- fiveGiving an example of lambda expressions in PHP is of course hurt - as far as I know they are neutered there. - Vladimir Gordeev