Home computickets What is lambda expressions?

# What is lambda expressions?

Author

Date

Category

What is it, why is it used? Examples …

Lambda functions are the functions that actually does not have a name. Thus, mathematics simplified to the inability to record function, but in general, lambda-calculus tried to formalize the calculations

``````λx.x
``````

`λ `– means that it is a lambda function. All that after it is a list of arguments, ideally any type, including another lambda function. After the point there is a “body function”, and then, in fact, there is an argument that will be transmitted. So

``````λx.x + 2 2 // returns 4
``````

Example more complicated:

``````λx.x.x 2 λy.y + 1 // Result 3
``````

Here, another lambda function `λy.y + 1 `, in which the parameter is transmitted 2. That is, any lambda function is a higher order function, can take another function as an argument and Return function:

``````λx.λy.y + x + 3 2 // return λy.y + 5, because X was equal to two.
λx.λy.y + x + 3 2 3 // Return 8. In fact, this carrying: First, the function takes the argument 2 and returns a function that takes another argument and returns the result.
``````

If it is interesting, I once wrote Similar things on C #

Now let’s see how all our examples will look at C #. Here, as a lambda function, I use Func , where T is the type of argument , and U – the type of return value:

``````1) Func & lt; int, int & gt; Func = x = & gt; x;
2) Var Result = New Func & Lt; int, int & gt; (x = & gt; x + 2) (2);
3) var result = new func & lt; func & lt; int, int & gt;, int & gt; (x = & gt; x (2)) (new func & lt; int, int & gt; (y = & gt; y + 1));
4) Var Result = New Func & Lt; int, Func & lt; int, int & gt; & gt; (x = & gt; y = & gt; y + x + 3) (2);
5) Var Result = New Func & Lt; int, Func & lt; int, int & gt; & gt; (x = & gt; y = & gt; y + x + 3) (2) (3);
``````

The complexity is only a clear indication of the type of arguments and the return value.

lambda – this is simple words that lies right in the variable if we want to record a function in the 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 a record of one line, it can be convenient to insert anywhere.

Lamd expressions – anonymous functions. Calked from mathematics, where a special form of recording functions was used, eliminating the ambiguity function / value of the function, etc. Thanks to the efforts of the vertellover (not HTML-, and the typographic stacks) from the \$ Hat XX \$ form, it was transformed in \$ WEDGE XX \$, well, and then already naturally – in \$ lambda xx \$

i.e. The value of the lambda expression is a function that can be applied to some argument / arguments.

Indicate the details (language in particular). Lambda Expression is Anonymous functors (variables containing a whole function). Lambda expression allows for a lexical context, in which this expression is used. in PHP, for example, such an expression is created as:

``````\$ func = create_function ("\$ a, \$ b", "Return \$ a + \$ b;"); // Lambda-Style Function
Echo \$ FUNC (1,2); // 3.
``````

more

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.