Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview: What is Lambda expression? [closed]

Tags:

lambda

I have had recently two telephone interviews.

In both interviews I have been asked as the last question to define a Lambda expression.

I claimed that Lambda expression is an unnamed method in place of a delegate. But somehow that wasn't enough.

I find it very hard to explain this precisely on a telephone interview.

Does anyone know better?

like image 854
Houman Avatar asked Sep 30 '10 15:09

Houman


People also ask

What is a closed lambda expression?

A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.

Why a lambda expression forms a closure?

a function that can be treated as an object is just a delegate. What makes a lambda a closure is that it captures its outer variables. lambda expressions converted to expression trees also have closure semantics, interestingly enough.

Is lambda expression a nameless suspension of code?

A lambda expression is a nameless suspension of code. Consider this anonymous "multiply two things" function (a.k.a. lambda expression), using some very non-specific notation.

What is lambda expression in Java interview questions?

Lambda expressions blends functional programming features with object oriented programming features of Java resulting in simplified and more powerful concurrency features. Lambda expressions are blocks of Java code that can be defined and passed as data which can be executed at a later time.


2 Answers

Lambda Expressions are nameless functions given as constant values. They can appear anywhere that any other constant may, but are typically written as a parameter to some other function. The canonical example is that you'll pass a comparison function to a generic "sort" routine, and instead of going to the trouble of defining a whole function (and incurring the lexical discontinuity and namespace pollution) to describe this comparison, you can just pass a lambda expression describing the comparison.

HOWEVER, this misses one of the most important features of Lambda Expressions, which is that they execute in the context of their appearance. Therefore, they can use the values of the variables that are defined in that context. This differentiates function-pointers from true lambda expressions. In languages supporting mutable variables, proper lambda expressions offer the power to change the values of those variables.

Lambda expressions appear (with different syntax) in all LISPs, Perl, Python, and sufficiently-recent versions of C++, Objective C, C# and Java 8, but notably not in C even though it has a way to deal with passing functions (or some excuse for them) around as parameters. They are a syntax element with particular semantics, and those semantics place more requirements on the runtime than C was designed to require.

like image 132
Ian Avatar answered Nov 25 '22 13:11

Ian


A lambda expression is a nameless suspension of code.

Consider this anonymous "multiply two things" function (a.k.a. lambda expression), using some very non-specific notation.

λ(x, y) -> x * y 

Your answer was very specific to a place where you've used lambdas (I'm guessing C#?), and I suspect the interviewer was asking for a more general understanding. The concept of a delegate, the language C#, and the idea of a method are all secondary to what a lambda is and how they work. You can compute using lambda expressions on paper, for instance, with no methods involved.

like image 44
Andres Jaan Tack Avatar answered Nov 25 '22 15:11

Andres Jaan Tack