45

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?

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
Houman
  • 64,245
  • 87
  • 278
  • 460
  • 3
    http://stackoverflow.com/questions/471502/what-is-linq/471592#471592 The part above "Let's start this exploration". – Amy B Sep 30 '10 at 15:53
  • 1
    All the LINQ methods are named. Some take unnamed methods as parameters, i.e. anonymous methods or lambda expressions – Greg Sep 30 '10 at 15:59
  • I can't believe the number of people who answered this obvious duplicate. – John Saunders Sep 30 '10 at 19:02
  • Don't forget that if HR is doing the interview, they may just have the answer and not understand the context enough to understand what your answer means to some extent. – JB King Sep 30 '10 at 19:04
  • I apologize for ruining the question. I have meant Lambda expression and my answers were also pointing to Lamda expression as you see above. I had somehow Linq on mind when I was asking the question. I have corrected it. Sorry again. – Houman Oct 01 '10 at 09:14
  • duplicate: [what-is-a-lambda](http://stackoverflow.com/questions/150129/what-is-a-lambda) – nawfal Dec 22 '13 at 02:27
  • lamda expression explained [here](https://chaseyourjava.blogspot.in/2017/11/lamda-expression-in-java-8.html) beautifully. – Jameer Mulani Dec 04 '17 at 16:34

7 Answers7

43

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.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
Ian
  • 4,421
  • 1
  • 20
  • 17
  • 3
    Lambda expressions are available in Java as of JDK-1.8 I believe. They look very similar to Lambda expressions in C#, except Java's "Fat Arrow" is thin; -> as opposed to => – Matthew Layton Jan 02 '14 at 17:09
18

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.

Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
  • 2
    I would get away from the particular notion of "code": lambda abstraction is about computation, not any particular language in which such computation might be described. It just happens that some programming languages include syntax to represent them more or less directly, just as most of the popular ones represent algebraic expressions directly. – Ian Jan 28 '14 at 21:33
2

To answer your revised question, for that, you answer is actually good. The only change I'd make is to stress the word "inline".

Lambda expression is an unnamed method that is written inline in place a where a delegate is needed.

James Curran
  • 101,701
  • 37
  • 181
  • 258
1

Maybe they just wanted to hear that LINQ was "Language-Integrated Query".

That being said, if they really want an explanation of "what" LINQ is comprised of, I would have probably included more information that you provided. Something like:

LINQ, or Language-Integrated Query, is a set of language additions and framework classes added in .NET 3.5 which enable a more functional approach to query operations. It's based upon extension methods for IEnumerable and IQueryable and their generic counterparts which allow deferred execution in LINQ to Objects and remote processing via IQueryable, as well as many other features. There were also language changes made to C# and VB.NET to support a more "natural" query syntax directly in the language.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

Well I said that Linq is an unnamed method in place of a delegate.

Actually, that's not LINQ at all, but a "lambda expression". And technically, LINQ doesn't even use them.

LINQ stands for "Language Integrated Query". Quite specifically, it's the "from...where.. select" keywords (i.e., the query syntax that is integrated into the language).

Now, to get those keyword to do things, a lot more was added to the language (and the CLR) (such as lambdas, extension methods, the Enumerable class etc).

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • I just realized I have ruined teh question. You are right. The question shoudl have been Lambda expression. At teh time of writing I had some how LINQ on mind. :( – Houman Oct 01 '10 at 09:11
1

You should look in Lambda Expression in msdn

x-code
  • 608
  • 1
  • 10
  • 30
Delashmate
  • 2,344
  • 5
  • 26
  • 40
0

They're probably looking for you to know that LINQ is the new DSL for querying IQueryable and IEnumerable objects. The "from ... where ... select ..." syntax, essentially. Knowing that it's implemented under-the-covers with lambdas and functional style would probably get you bonus points.

Mark
  • 11,257
  • 11
  • 61
  • 97