Lambda Functions - Does your language support it?

What are Lambda functions? Basically lambdas are functions that can be treated as anonymous or as objects. It depends on the language that is being used. They are very useful and at the core of the Functional Programming paradigm. So what languages can you create lambas in? Here is a few;

JavaScript

  1. var foo = function(x, y){
  2.     return x + y;
  3. }
  4.  
  5. alert(foo(2, 4));

PHP

  1. $foo = create_function(‘$x,$y’, ‘return $x + $y;’);
  2.  
  3. echo $foo(2,4);

Python

  1. foo = lambda x, y: x + y
  2.  
  3. print foo(2,4)

These are of course very simple examples and don’t show the full potential. I’ve been a fan of lambdas for a while now and wanted to see what languages supported them. If you know of others you want me to add, post them in the comments. I know many other languages can, I just posted up the languages I’m familiar with.

I think out of all the examples I’ve looked at so far I like the style of them most in either JavaScript of Python.

Tags: , ,

3 Responses to “Lambda Functions - Does your language support it?”

  1. Peter Says:

    Fogive me for my slowness on the uptake, but I don’t get the Python one.You first define the function with
    def lambda x, y: return x + y

    then you call foo(2,4)
    - how does Python know that the previously defined function is the one you want to execute when you call foo? (assume there was more than one defined lambda function)
    P.S. note the date and time - your blog is stopping me from studying ECO ;-)

  2. Dougal Says:

    Fixed it, I seemed to have copied the wrong example into my post. oops. The python example now makes sense!

  3. Reading List: 15 & 16June08 » Karl Katzke | PHP, Puppies, and other Geekery Says:

    [...] Lambda Functions: Does your Language Support it? [...]

Leave a Reply