In Python you can use lambda expressions to avoid the creation of temporary, one-time use functions. Look at the following example.
def power(x): return x**2 l1 = map(power, range(4))
If you don’t need the power() function in another place, it’s a bit annoying to write it just to use it in the call to the map() function. However, you can use a lambda expression instead as follows.
>>> map((lambda x: x**2), range(5)) [0, 1, 4, 9, 16] >>>
Now how does this look in functional programming languages. Let’s have a look at lambdas in Haskell.
Prelude> map (\x -> x^2) [0..4] [0,1,4,9,16] Prelude>
Hint: Haskell professionals would use (^2) instead the lambda here. I’m just trying to illustrate the use of lambdas here. What about F#? It looks as follows.
List.map (fun x -> x**2.0) [0.0; 1.0; 2.0; 3.0; 4.0]
Well, this looks a bit different than the examples because, as the documentation for F# says: “The exponentiation operator works only with floating-point types.” Hhm, really?