What is a lambda language?

sushil bharwani picture sushil bharwani · Oct 5, 2010 · Viewed 26.7k times · Source

I was reading "JavaScript: The Good Parts" and the author mentions that JavaScript is the first of the lambda languages to be launched.

JavaScript's functions are first class objects with (mostly) lexical scoping. JavaScript is the first lambda language to go mainstream. Deep down, JavaScript has more in common with Lisp and Scheme than with Java. It is Lisp in C's clothing. This makes JavaScript is remarkably powerful language.

I didn't get what is a lambda language. What are the properties of such a language and how is it different from languages like Java, C, C++ and Php?

Answer

Juan Mendes picture Juan Mendes · Oct 5, 2010

A lambda language, in simple terms, is a language that allows passing a function to another function, where the function is treated as any other variable. Also, you should be able to define this function to be passed anonymously (or inline). PHP 5.3 added support for lambda functions. Was JavaScript the first mainstream language? Lisp has been widely used in educational settings before JavaScript and also in customizing our beloved Emacs http://www.gnu.org/software/emacs/manual/html_node/eintr/

Here's an example

function applyOperation(a, b, operation) {
  return operation(a,b);
}

function add(a,b) { return a+ b; }
function subtract(a,b) {return a - b;}

// Can be called like
applyOperation(1,2, add);
applyOperation(4,5, subtract);
// Anonymous inline function
applyOperation(4,7, function(a,b) {return a * b})

How is it different from C? In C, you can pass pointer to functions, but you can't define it inline anonymously.

In Java (before version 8), to achieve the same effect, you must pass an object that implements an interface, which actually can be defined anonymously inline.