## Difference between LINQ and lambda expression

I have stuck at this field for a long time. Today it suddenly becomes clear to me: LINQ and lambda expressions are to very different things (though they complements each other on many occasions).

## Lambda expression

Lambda expression, to sum up, is only about the operator “=>” (read as “goes to”). It serves as a shorthand for anonymous function (usually used to write short expressions like “x => x % 2 == 0” to find even numbers). It will be automatically typed as either an Expression, a delegate or a Func depending on where it is used.

## LINQ

LINQ is like SQL for objects. It can query arrays, list or any other enumerable objects. It’s sort of an advanced for loops with SQL-like operations like sorting, grouping and of course, lambda expression support. Usual syntax is “from <iterator name> in <data> where <condition on iterator> select <iterator>”

Consider the following problem: You need to calculate how many dots are there in a square triangle for a specific width, for example

```.
..
...```

Has an area of 6

```.
..
...
....```

Has an area of 10
The catch is, you are not allowed to use the mathematical formula to calculate the number instantly, so there’s two ways remaining to solve this problem:

• Use two for loops
• Recursion

We will follow the second direction because it’s probably shorter to write, so someone wrote

```public class TrianglePrinter {
static int getTriangleArea(int width) {
int result = 0; // Default return
if (width > 0) { // Stop condition
result = getTriangleArea(width - 1) + width; // Split the problem
}
return result;
}

public static void main(String[] args) {
System.out.println(getTriangleArea(4));
}
}```

This can be “optimized” to become a one-line method, short code gives longer lives!

```public class TrianglePrinter {
static int getTriangleArea(int bottomWidth) {
return (bottomWidth > 0) ? getTriangleArea(bottomWidth - 1) + bottomWidth : 0;
}

public static void main(String[] args) {
System.out.println(getTriangleArea(4));
}
}```

Then when it is compacted (like, in Javascript…)

```public class TrianglePrinter {
static int g(int w) {
return (w > 0) ? g(w - 1) + w : 0;
}

public static void main(String[] args) {
System.out.println(g(4));
}
}```

“Which programming language is the best?” It’s the kind of question that you will keep getting from aspiring programmers (maybe with some minor variations but it’s essentially the same problem). It’s a difficult question to answer. Although as old as the question itself, the debate is still alive today as evidenced by:

this
this
… and this

I’ll have to warn you, if (just in case) you don’t have a sense of (developers’) humor: they are just jokes, really. There’s no such thing as a “best” language. Everything is there for a reason, including programming languages. So the answer to the question above is: it depends on what you really want to do. All the languages involved in the debate are general-purposed, which means they can be used to do just about anything. For example, you may think that C++ could never be used to produce web pages, Actually, you can always write a C++ program to listen for  HTTP GET requests and then send HTML to the client. Alternatively, if you are Microsoft-savvy, you can always write an ISAPI application for ISS to intercept requests and process them.

A second thing worth noting in the arguments used is the ability to bootstrap the language with itself, that is, you can make a compiler for a language by writing code in that same language. I agree with a contributor on Wikipedia:

The definition of a language by a self-interpreter is not well-founded (i.e., cannot be used to define a language), but a self-interpreter tells a reader a lot about the expressiveness and elegance of a language. It also enables the interpreter to interpret its own source code, the first step towards a reflective interpreter.

Why bother really? PHP and Ruby did their jobs well with the compilers written in C.It’s a waste of resources both in manpower and computing power’s term trying to reinvent the wheel.

Someone have sum up the desirable characteristics for an ideal language

The language should

• Have an intepreter for rapid prototyping, ease of debugging, and maximum fun.
• Have a native code (not just byte code) compiler that produces fast code that can be run stand-alone or be called from the interactive environment.
• Have good support for vectors, multi-dimensional arrays, strings, hash tables, etc. in the standard library.
• Have a free implementation.
• Work under Linux and Windows (so I can transfer code easily between my desktop and my laptop).

But that’s a person dream. For most of us who doesn’t have quite enough formal description enough to write anew language, we’ll have to make it with existing ones. Below is my stance on some of the (more popular) languages but if you wont a more detailed comparison of each language’s characteristics, this may be interesting to you.

C and C++

They are used to make just about anything. The difference between them is C++ have a more extensive set of libraries, which is expensive in term of storage space to implement so it’s not as popular on embedded devices as C. Their application may range from programming microchips to a whole operating system (you name it. Linux, Windows and Mac may seems all different but they are all in C and C++).

A common misconception is that there’s something a language cannot do. If you haven’t seen anyone write web pages in C and C++ then try to type your DSL modem’s address into your browser. You should see a web page that allow you to configure the device. Such a device wouldn’t have enough processing power for the additional abstraction layer of a web server’s but instead its role is encompassed by the firmware. The firmware itself, is of course, written with you-know-what.

In short, they are good if you are going to deal with hardware programming.

C#/Java

Considered high-level languages, they are the most promising platform to implement 4th generation languages. Notice that I wrote “platform” and not “language”. This is because C#/Java is merely an interface for a lower language, the common language for .net and bytecode for Java. The additional abstraction gives the developer the freedom to move the application across platform. Input and Output are not necessarily specific devices anymore. You can output to a screen or a printer, it’s the same to the language. You can have a language run on a mobile device or a PC without changing anything in the code, the virtual machine took care of it. You can use a module in a desktop application in a web service without any modification, increasing reusability and reduce the amount of work required to write code.

Because these language was designed later than C and C++ and targeted developer instead of computer scientists, you are no longer required to remember the difference between pointer and variable, don’t have to freak out with memory management and other trivial matters. Package and variable names are also neatly named according to a naming standard, since these language is controlled by a singe entity.

In a nutshell, choose them if you want to properly apply development paradigms and produce quality software that run fast and reliable, anywhere. But you will grow dependent on the managing companies though (namely Microsoft and Oracle)

Ruby and Python

Developer-to-developer languages. They are written with only one goal: get things done, as fast as possible. And as such they employ a vast number of libraries and add-on (and you will have to remember all their syntax). Professional developer seems to have no problem with that though, so they are quite popular among the web community. That’s why ruby thrives on rail and Google is built off Python. They have a different syntax from C (which is also different from all the languages above) though, so switching to them may be a problem for newcomers.

PHP

Started as a collection of script and the result of a developer disgruntled with his boss, PHP is released for free because it can’t be sold at first but accidentally, it’s just that that make it popular. Because it’s free, it’s a convenient platform to implement open-source projects that the user can download a whole platform off the Internet and have it running free-of-charge. Googling for WordPress and Joomla is enough to demonstrate PHP’s popularity.

Since PHP seems to have matured (its main version number stayed at 5 for two years now and had no significant changes since), it’s a good language to learn if you are intending to go freelance serving small and medium enterprises (SMEs) who doesn’t have enough financial ability to employ powerful servers and expensive softwares.

http://www.cs.ubc.ca/~murphyk/Software/which_language.html