Jump to content

What is a Lambda in programming?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
tobro

tobro

    Newbie

  • Members
  • Pip
  • 2 posts
In programming a lambda usually means a named expression of some kind? The
expression itself is then referred to as a lambda-expression?

Have I captured the concept (described below)? Am i close?

Examples written in pseudocode...

Example 1:

myLambda = { x + y; }


This would create a lambda that gives the result from adding variables x and y.

It will/could resolve to a different result depending on the context in wich
it is used.

Example 2:

if (doThis == True)

{

    x = "Hello ";

    y = "World!";

    Print(myLambda); // -> "Hello World!"

}

else

{

    x = 2;

    y = 5;

    Print(myLambda); // -> "7"

}


You could make a lambda that takes arguments:

Example 3:

myOtherLambda = (x) { x * y; }


newLambda = myOtherLambda(myLambda);  // -> { { x + y } * y }



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
A lambda (expression) is basically an anonymous function that contains expressions or statements, and can be used to create expression tree or delegate types.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
A Lambda is, as Nullw0rm already stated, essentially an anonymous function. They're not particularly useful in procedural or object oriented languages, since you can't pass a function as an argument (only function pointers at best). As such, usually functional languages take advantage of lambda expressions. For the purposes of my example, I will be using Haskell...

First, an explanation of Haskell functions is necessary, they look like this:
{- Things nested in these brackets are comments. -}
functionName arg1 {- arg2..argN -} = {- Function body here, something like... -} arg1 + 5
That is a function that takes whatever arg1 you get to the function and adds 5 to it, which is the result this function resolves to. All right, take this concept and let's make an anonymous function!
main = putStrLn . show $ map (\x -> x + 5) [1..10]
{- This main function will resolve by first starting with map, which is a "higher-order function", or a
   function that accepts another function as an argument. Map takes the function passed to it and
   applies it to each element of the list, returning a new list with the modified elements. The function
   we pass to it is a lambda function (signified by \) along with a list from 1 to 10. This should resolve
   to a list from 6 to 15.

   The main function will then run show and putStrLn on that result, which should print to the screen
   "[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]". -}
The main point to take from this is my use of the lambda function. I didn't have to define a whole function with a name, since it was only really necessary to use once, or to be passed as an argument to another function, so there was no need to do so. Because of function currying, it's actually shorter to do what I did with map, but the point was to show lambda functions.

So your example was inaccurate, simply by the fact that the function was named, and lambda functions are not. I tried to explain what a lambda expression was in programming, and hopefully succeeded. If there is any confusion, don't be afraid to ask.
Wow I changed my sig!

#4
Raynes

Raynes

    Newbie

  • Members
  • PipPip
  • 17 posts
To add to ZekeDragon's excellent post, I'll show you why lambdas (anonymous functions) are useful. While he used Haskell as his example language, I'll use Clojure.

In Clojure, a lambda is created with the `fn` special form, so it's different from Haskell. You can create one like this:
(fn [args] body-of-function)
Now, we're using a functional programming language, so functions can be passed around like any other value. You can pass functions to other functions, and you can store functions in data structures and any number of awesome things. There is a shorter way to create lambdas in Clojure using the #() reader macro. You could use it to create a lambda like this:
#(do-something % %2 %3)
where %, %2, and %3 are the first three arguments to the function.

Let's use the map function as an example. The map function takes a function that takes a single argument and a sequence. It moves through the entire sequence one element at a time. It applies the function to each one of these elements, and places the return value in the place of the element in the sequence. Once it's finished, it returns the newly mapped sequence.

Now, what if you just wanted to square each number in a sequence? If you don't square anything else in your code, it wouldn't make sense to create a top-level square function when only one function is going to use it, would it? Instead, we can just use an anonymous function:
(map #(* % %) (range 100)) ; Here we're using the shorthand for creating lambdas. We're also using the range function. It just generates a sequence of numbers from 0 to n (n is 100 here)
;We could also do it like this:
(map (fn [n] (* n n)) (range 100)) ; This works the same way, but uses the fn special-form directly.

And that's it.

Another reason lambdas are useful are for times where you need to use the same short function multiple times within the scope of another function or something. If the function is only used within that scope and nowhere else in your code, it doesn't make sense to define it as a top-level function. Alternatively, you can just give that function a name, locally:
(let [square #(* % %)] ; We created a local square function using a lambda
  [(map square (range 100)) (map square (range 100 301))])

Anonymous functions are very useful things, and lots of languages support them. Even Java will likely have some support for lambdas and closures in the upcoming JDK7.

I hope this post was helpful. <3

#5
tobro

tobro

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you very much for the answers, they are awesome! The lambda concept now seems to me to be the notion of a function literal more or less.

#6
JCoder

JCoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 245 posts

Quote

They're not particularly useful in procedural or object oriented languages

They ARE very useful in some object-oriented languages. See OCaml or Scala.