views:

254

answers:

5

I was reading "javascript good parts" author mentions that javascript is 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++ php?

+4  A: 

I've never heard anyone use the term "lambda language," and the only plausible definitions I can think of would exclude JavaScript as "the first."

That said, I suspect he may mean either:

  • Functional languages: a class of languages in which computation is (or can be) modeled as a stateless composition of (possibly higher-order) functions. LISP, Scheme, ML, Haskell, etc. are frequently ascribed to this class, although several of these are more properly mixed paradigm or "functional optional" languages. Javascript arguably contains the necessary features to make a "functional style" of programming possible.
  • Languages which allow the creation of anonymous functions (using the function syntax in JavaScript; this is written lambda in many languages, hence possibly "lambda languages."

Both usages are derived from the use of the greek letter lambda to denote function abstraction in the lambda calculus, the model of computation devised by Alonzo Church and upon which functional programming is based.

Edit: looked at Google Books result---"first to go mainstream"; well, that's arguable. I'd put forward that LISP was at one point at least reasonably mainstream. It's a fair point though, JavaScript's semantics are directly inspired by Scheme and it certainly reached a larger audience than any other language that can make similar claims.

Derrick Turk
please see the link posted by Lucas Jones Google book link where he takes to the exact page i am talking about. thanks for ur answer.
sushil bharwani
The reason JavaScript is described as being 'the first' is because the statement is qualified with 'to go mainstream'. I think it's pretty clear that JavaScript is indeed the first functional-style language to be used by every-day programmers at every-day jobs.
thenduks
what do you mean when you say "functional-style" of programming language. I am from a java background and trying to learn javascript how is it different.
sushil bharwani
@Derrick please explain "Functional languages: a class of languages in which computation is (or can be) modeled as a stateless composition of (possibly higher-order) functions"
sushil bharwani
It's a huge topic to explore. Last year I was where you are now: I had found Crockford's essays and lectures and wanted to understand closures. I would recommend you start with Wikipedia, then move on to a few essays: John Hughes "Why Functional Programming Matters" http://www.scribd.com/doc/26902/whyfp, Slava Akhmechet "Functional Programming for the rest of us" http://www.defmacro.org/ramblings/fp.html There is a wealth of books to expand your mind: check out the Little Schemer to get a feel for Scheme, and then move on to the Structure and Interpretation of Computer Programs.
spacemanaki
Of course, there are also loads of questions on SO with great explanations on closures, lambda expressions, etc ...
spacemanaki
@spacemanaki I wish i would be able to keep clicking upvotes for posting all the links many thanks for ur posts...
sushil bharwani
No problem at all!
spacemanaki
@sushil: there's a lot of introductory literature on the topic. In general terms though, it's a style of programming that emphasizes treating programs as mathematical objects rather than sequential instructions or interactions between abstract objects. For example, functional languages will prefer recursion to iteration, make use of immutable data structures, and use higher-order functions in preference to "function objects" or "strategy patterns". Higher-order functions meaning functions which may operate on (as arguments) or yield (as return values) other functions.
Derrick Turk
A: 

From wikipedia: In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus. An example of this use of lambda in the Python language is this section of computer code that sorts a list alphabetically by the last character of each entry:

>>> list = ['woman', 'man', 'horse', 'boat', 'plane', 'dog']
>>> sorted(list, key=lambda word: word[-1])
['horse', 'plane', 'dog', 'woman', 'man', 'boat']

* In the C# programming language a lambda expression is an anonymous function that can contain expressions and statements
r3nrut
+1  A: 

He refers to Lambda calculus.

Lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion. [...]

[...] with untyped lambda calculus being the original inspiration for functional programming, in particular Lisp, and typed lambda calculi serving as the foundation for modern type systems.

BenoitParis
+3  A: 

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 added support for lambda functions. Was JavaScript the first mainstream language? Lisp has been widely used before JavaScript.

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;}

and 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, to achieve the same effect, you must pass an object that implements an interface, which actually can be defined anonymously inline.

Juan Mendes
"Lisp has been widely used" by whom? I've always known about it, since professors would always discuss it, but in practice, I've never met anyone who has used Lisp. I would say that anyone could find someone they know who has used Javascript, which makes Javascript much more "mainstream".
palswim
"PHP 5 added support for lambda functions". Actually iy was PHP 5.3 that introduced lambda functions.
Crozin
I've met few people that used it in a business context, but like Pascal once was, it's widely used in educational settings.
Juan Mendes
A: 

I've seen a lambda defined as an anonymous function and as a reference to a function. Javascript supports both:

setTimeout(function(){ /* an anonymous function */ }, 100)

var f = function(){ /* function ref */ }

This is where JS gets a lot of its power and flexibility. Java supports the first to some extent (anonymous interface implementations), but not that latter.

Its unclear to me which (or both) of these is the proper definition of a lambda.

JS is definitely not the first language to support these features. Going from memory, I think its smalltalk that language enthusiasts always rave about supporting lambdas.

BTW: In Java, an anonymous class is usually used to pass in a class definition on the fly for an argument (used a lot in swing). Something like this (from memory, not compiled):

someGuiContainer(new WidgetInterface()
      {
           public void importantMethodToDefine(){
             // Handle having the method called in my special widget way
           }
        }
)
mtyson