5

I'm trying to learn pure functional programming. But this code is puzzling me particularly the second line. I do not understand how the value 2 is passed to the variable x. Can somebody explain this nested lambda behaviour?

>>> square_func = lambda x: x**2
>>> function_product = lambda F, m: lambda x: F(x)*m
>>> square_func(2)
4
>>> function_product(square_func, 3)(2)
12
kmario23
  • 42,075
  • 12
  • 123
  • 130

5 Answers5

2

The inner lambda creates a function when the outer lambda is called. The outer lambda returns this function. This function is then called with the argument 2.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
2

A good place to start would be to apply type to your definitions and see if it clarifies things. Also, I can't help but remark that something like Haskell would be a nicer place to start if you are interested in functional programming, even if you do not plan on using the language. That being said, here is what you get:

In [13]: type(square_func)
Out[13]: function

In [14]: type(function_product)
Out[14]: function

In [15]: type(square_func(2))
Out[15]: int

In [16]: type(function_product(square_func, 3))
Out[16]: function

In [17]: type(function_product(square_func, 3)(2))
Out[17]: int

So the puzzling part is the return type of function_product(square_func, 3), which is a function itself, one that is presumably intended to take a single number and return a single number. You could write it as:

f = function_product(square_func, 3)
f(2)
  • So, when `function_product` is called `square_func` is binded to `F` and `3` to `m`, which then returns a function which is binded to `function_product` which when called with `2`, `x` is assigned this and `square_func` is called, which returns `4` and this in turn is multiplied with `m` which is `4`. So it'll finally return `12`. Is my understanding/interpretation correct? – kmario23 Apr 03 '16 at 22:30
1

function_product asks for a function and a variable, you passed square_func and 3. Then, the inner function of function_product takes the result of the function you passed (square_func) and multiply it by the variable you passed, so:

square_func(2)*3 = 12
João Paulo
  • 5,109
  • 3
  • 37
  • 64
1

function_product(square_func, 3) returns other lamba function which can be defined implicitly like this:

lambda x: square_func(x)*3

next by calling the other lamba function you pass 2 to the variable x

apr
  • 360
  • 1
  • 5
1

It's kind of a rule (or convention), if you follow the style guide, not to use lambda in the context you have used. The reason for this is exactly what made you turn to the internet in confusion. The flags are:

  1. You are giving an anonymous function a name
  2. There are multiple lambda. For a language that does not have nested anonymous functions, this is a code smell. There must be a better way

How about writing this way and reaping the benefits:

def function_product(F, m):      # returns inner, a function
    def inner(x):                # takes x, and closes over F and m from 
        return F(x)*m            # outer scope, hence a closure
    return inner

See, everything is clear because it's more readable now. Avoid lambda, maybe except for callbacks,like in sorted(). Normal functions are objects, just like 10 and hello are.

Always do import this. :)

C Panda
  • 2,749
  • 1
  • 9
  • 11