Recursion without Referencing and Assignment - python

The problem is given a recursive function in python (or similar), can it be re-written it so that it doesn't reference itself. I've made a simple example which applies to the problem. Of course, making it a non-recursive function is not allowed. It must still do the same recursion process.
def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
It's also equivalent to a short-hand.
fact = lambda n: 1 if n == 0 else n * fact(n - 1)
In the example, I shouldn't call fact inside the function definition.
Edit:
Additional constraints:
Assignments must not be necessary for the solution to work.
One solution (from the comments) without the additional constraint is to create two functions a and b which call each other alternately and do factorial effevtively. This is not allowed because it requires that you assign two functions in two variables.
A quick example of a function that doesn't need assignment is
f = lambda x: x + 1
For it to execute on the arugment 55, I could just write
(lambda x: x + 1)(55)
So assignment was not necessary.
Any hints on this? Or am I being tricked to an impossible problem?

You can use something like lambda calculus to avoid assignment and self reference, replacing both with access to an argument of an anonymous function. For example:
fact = (lambda f: f(f))(lambda f: (lambda n: n*f(f)(n-1) if n else 1))
Tested in Ideone.
Some details below for further background.
I know lambda calculus is famous for being a powerful (Turing complete) yet minimalistic “programming language”. It only uses identifiers for variables, which can be either bound (pretty much function arguments) or unbound (mostly relevant when talking about parts of an expression). So it felt like a good starting point.
The canonical way to express recursion in lambda calculus is using a fixed point combinator. While that combinator can be expressed naively in Python syntax, eager evaluation leads to infinte recursion.
The code at https://rosettacode.org/wiki/Y_combinator#Python mentioned in comments avoids this infinite recursion by delaying one of the recursive calls till the function actually gets called. But I would prefer leaving detailed explanation of that approach to a separate answer.
What is the core idea of expressing recursion in lambda calculus? Passing a function as an argument to itself. So I started with this:
lambda f: f(f) # λ f.f f
I need to pass that function another function that takes a function as a value. Like lambda f: …. And the result of that call should be a function that should take an n as an argument to compute the factorial. My first approximation was thinking of f itself as an expression for the recursive call, so I had this first:
(lambda f: f(f))(lambda f: (lambda n: n*f(n-1) if n else 1))
But then I realized this was wrong: f itself is not the recursive call, since f is the function that takes an argument f. So f(f) is the recursive call, leading to the solution I printed in the beginning.

This is most likely absolutely not what the question is about, but with some inspect/types magic you can reconstruct the function being called...
import types, inspect
def fact(n):
frame = inspect.currentframe()
fn = types.FunctionType(frame.f_code, frame.f_globals)
if n == 0:
return 1
return n * fn(n - 1)
print(fact(10))

Related

Python sum(generator): calling an external function

take the code:
SUM = sum(x() for x in xs)
I am writing some code that is in need of calling another function before each x() such that x() will compute the right value
is the only way to do this like so?
for x in xs: x.pre()
SUM = sum(x() for x in xs)
or
SUM = 0
for x in xs:
x.pre()
SUM += x()
incorporating x.pre into x is obviously possible but would make the real code exceptionally ugly and hard to read. is there some way of using generator expressions I am unaware of that would allow what I am trying to achieve?
eg:
SUM = sum(x(), x.pre() for x in xs)
thats obviously just an non-summable tuple generator
I would simply use the for-loop you already presented.
There are ways to do what you want in other ways. For example you could use functools.reduce with a customized function here instead of sum :
def pre_then_add(accumulated, new_one):
new_one.pre() # do some stuff so we get the right value
return accumulated + new_one() # add the value to the accumulated sum
and then it's just a matter of calling reduce:
import functools
functools.reduce(pre_then_add, xs, 0)
Note that this requires to give a "base value" otherwise the function is too simple but also that reduce with a customized function is not necessarily the most efficient or elegant way.
As pointed in the comments (thanks to #SteveJessop) another possibility using sum would be:
def pre_then_call(x):
x.pre();
return x()
sum(pre_then_call(x) for x in xs)
But just to repeat it: I would use the explicit loop. Those are far easier to understand in terms of what you are doing and why.

Understanding nested lambda function behaviour in python

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
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.
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)
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:
You are giving an anonymous function a name
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. :)
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
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

Please help me translate the following lambda to human language

What do the following expression actually does?
list = [lambda n=n: lambda x: x+n for n in range(10)]
More specifically:
What does n=n mean?
What will be the content of 'list'?
What will be the output of
print(list[0](14)) and print(list[0]()(14))
and why?
What does n=n mean?
lambda lets you define functions that take parameters, just like def. And those parameters can have default argument values. So, lambda n=n: is the same as def foo(n=n):.
In fact, when faced with an expression that's too complicated for you to read, it's often worth unpacking into simple statements:
list = []
for n in range(10):
def spam(n=n):
def eggs(x):
return x+n
return eggs
list.append(spam)
Now, why would you want to create a parameter named n with default value n? Why not just lambda:? The official FAQ explains this, but let's try to summarize.
If you just write this:
funcs = [lambda: n for n in range(10)]
… what you get is 10 functions of no parameters, that are all closures over the same variable, n. Because n has the value 9 at the end of the loop, when called, they're all going to return 9.
But if you do this:
funcs = [lambda n=n: n for n in range(10)]
… what you get is 10 functions of one optional parameter n (which hides the closure n from view), whose default value is the value of n at the time each function was defined. So, when called with no arguments, the first one will return 0, the second 1, and so on.
In your case, of course, the functions aren't just returning n, they're returning a function that takes a parameter, adds n to it, and returns the result. But the idea is the same; you want them to return different functions, which add 0, 1, … 9 to their arguments, not all return equal functions that all add 9.
What will be the content of list?
list will be 10 functions of one optional parameter whose default values range from 0 to 9, each of which returns a function of one parameter. That returned function is a closure over the value of n from the outer function. So, when it's called, it returns its argument, x, plus the n variable that ranges from 0 through 9.
What will be the output of
print(list[0](14))
Here, you're calling the first outer function, list[0], with the argument 14. So, instead of its default value 0 for n, it's going to have 14. So, what you'll get is a function that takes one argument and adds 14 to it. But it will print out as something like:
<function <listcomp>.<lambda>.<locals>.<lambda> at 0x105f21f28>
That long mess is Python 3.4+ trying to be helpful by telling you where to find the function definition. Usually, when a function is nested this deeply, most of the steps along the way have names. In this case, you've got three layers of anonymous functions, so none of the names are very useful…
In order to see it do anything useful, you'll have to call it:
print(list[0](14)(20))
And this will give you 34.
You could also use the inspect module, or just dir, to poke around inside the function. For example, print(list[0](14).__code__.co_freevars[0], list[0](14).__closure__[0].cell_contents) will tell you that it's stashed the number 14 under the name n for use by its internal function.
…
print(list[0]()(14))
Here, you're again calling list[0], but this time with no argument, so its n gets the default value of 0. So, it returns a function that adds 0 to its argument. You then call that function with 14, so you get 14.
To answer the last part first:
In [1]: list = [lambda n=n: lambda x: x+n for n in range(10)]
In [2]: print(list[0](14))
<function <lambda> at 0x7f47b5ca7cf8>
In [3]: print(list[0]()(14))
14
Obtained by running the code. list bad name by the way as list is a python builtin gives you 10 lambda functions that don't do much - the first will return the original argument x, the second the argument + 1, ect. as n is stored as the index of the lambda by n=n local to that lambda.

Python lambda closure scoping [duplicate]

This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 6 months ago.
I am trying to use closures to eliminate a variable from a function signature (the application is to make writing all the functions needed for connecting Qt signals for an interface to control a largish number of parameters to the dictionary that stores the values ).
I do not understand why the case of using the lambda not wrapped in another function returns the last name for all cases.
names = ['a', 'b', 'c']
def test_fun(name, x):
print(name, x)
def gen_clousure(name):
return lambda x: test_fun(name, x)
funcs1 = [gen_clousure(n) for n in names]
funcs2 = [lambda x: test_fun(n, x) for n in names]
# this is what I want
In [88]: for f in funcs1:
....: f(1)
a 1
b 1
c 1
# I do not understand why I get this
In [89]: for f in funcs2:
....: f(1)
c 1
c 1
c 1
The reason is that closures (lambdas or otherwise) close over names, not values. When you define lambda x: test_fun(n, x), the n is not evaluated, because it is inside the function. It is evaluated when the function is called, at which time the value that is there is the last value from the loop.
You say at the beginning that you want to "use closures to eliminate a variable from a function signature", but it doesn't really work that way. (See below, though, for a way that may satisfy you, depending on what you mean by "eliminate".) Variables inside the function body will not be evaluated when the function is defined. In order to get the function to take a "snapshot" of the variable as it exists at function-definition time, you must pass the variable as an argument. The usual way to do this is to give the function an argument whose default value is the variable from the outer scope. Look at the difference between these two examples:
>>> stuff = [lambda x: n+x for n in [1, 2, 3]]
>>> for f in stuff:
... print f(1)
4
4
4
>>> stuff = [lambda x, n=n: n+x for n in [1, 2, 3]]
>>> for f in stuff:
... print f(1)
2
3
4
In the second example, passing n as an argument to the function "locks in" the current value of n to that function. You have to do something like this if you want to lock in the value in this way. (If it didn't work this way, things like global variables wouldn't work at all; it's essential that free variables be looked up at the time of use.)
Note that nothing about this behavior is specific to lambdas. The same scoping rules are in effect if you use def to define a function that references variables from the enclosing scope.
If you really want to, you can avoid adding the extra argument to your returned function, but to do so you must wrap that function in yet another function, like so:
>>> def makeFunc(n):
... return lambda x: x+n
>>> stuff = [makeFunc(n) for n in [1, 2, 3]]
>>> for f in stuff:
... print f(1)
2
3
4
Here, the inner lambda still looks up the value of n when it is called. But the n it refers to is no longer a global variable but a local variable inside the enclosing function makeFunc. A new value of this local variable is created every time makeFunc is called, and the returned lambda creates a closure that "saves" the local variable value that was in effect for that invocation of makeFunc. Thus each function created in the loop has its own "private" variable called x. (For this simple case, this can also be done using a lambda for the outer function --- stuff = [(lambda n: lambda x: x+n)(n) for n in [1, 2, 3]] --- but this is less readable.)
Notice that you still have to pass your n as an argument, it's just that, by doing it this way, you don't pass it as an argument to the same function that winds up going into the stuff list; instead you pass it as an argument to a helper function that creates the function you want to put into stuff. The advantage of using this two-function approach is that the returned function is "clean" and doesn't have the extra argument; this could be useful if you were wrapping functions that accepted a lot of arguments, in which case it could become confusing to remember where the n argument was in the list. The disadvantage is that, doing it this way, the process of making the functions is more complicated, since you need another enclosing function.
The upshot is that there is a tradeoff: you can make the function-creation process simpler (i.e., no need for two nested functions), but then you must make the resulting function a bit more complicated (i.e., it has this extra n=n argument). Or you can make the function simpler (i.e., it has no n=n argument), but then you must make the function-creation process more complicated (i.e., you need two nested functions to implement the mechanism).

How lambdas work?

I'm learning python using the tutorial on the official python website and came across this example:
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
Where does x get it's value from? I'm not familiar with how lambda works, I understand anonymous functions just fine from javascript but this has me stumped. Anyone care to shed some light? I'd be grateful.
Consider this. f is the object created by the make_incrementor function.
It is a lambda, an "anonymous function".
>>> f= lambda x: x+42
>>> f(10)
52
The value for x showed up when we applied f to a value.
First off, here's a translation into JavaScript:
function makeIncrementor(n) {
return function (x) {return x + n}
}
A lambda expression consists of three parts.
The word lambda (In JavaScript, the word function)
A list of parameters (In JavaScript, the part in (...))
Some expression to evaluate (In JavaScript, the part in {...}, automatically returned)
The list of parameters is the part before the colon. These parameters are visible within the scope of the expression. Whatever the expression evaluates to is returned as the result of the function.
Unlike in JavaScript, a lambda can only contain a single expression; you can't make an anonymous multi-line function with lambda.
Disclaimer: I have pretty much no Python background. This is going off my Scheme/lambda calculus knowledge.
make_incrementor defines a function to make functions. make_incrementor(42) returns a function with x bounded to the lambda, and n with a value of 42. When you call the function f, the argument of f replaces the bound variable.
lambda brings some lambda calculus to Python. In essence, this is what's happening: normally in lambda calculus a statement would look something like this:
sum = add 3 4
This would pass 3 and 4 to the add function and store the result in sum. However, we could also write something along the lines of add 3. Now, since add expects two arguments, we now get an object (or function, if you will) expecting one argument. That function will then call add with 3 as its first argument and whatever we pass it as the second argument. We can now do this:
func = add 3
sum = func 4
This will be equivalent to the previous example. However, you can now use func whenever you want to add 3 to something. (Granted, this doesn't seem useful in this example, but in more complex situations it is).
All this is closely related to currying, something very central in most functional languages. If you're interested in lambda calculus and its similarity to regular mathematics, I highly recommend that you take a look at Haskell.
When make_incrementor() is called it creates and returns a lambda function at that time. In the process the value of the argument n gets stored or remembered in the function object created. If you called it again with a different n a different function would be returned.
x gets its value from the parameter used to call f.
n gets its value from the parameter used to call make_incrementor.
make_incrementor is a function that returns a function:
def make_incrementor(n):
return lambda x: x + n
When we call make_incrementor, we bind the parameter 42 to n, and return the concrete function:
f = make_incrementor(42) # fill in n at this point and return a function
This function we will return will therefore look like:
lambda(x): x + 42
f is bound to the returned function, so f will conceptually look like:
def f(x):
return x + 42
When we call f, we provide the value for x
f(1) # x == 1
f(2) # x == 2
Here's how I understand lambda:
When we write x2, we often confuse two distinct ideas. Consider:
x2 has an odd number of factors when x is an integer.
x2 is larger than x when x>1.
x2 has derivative 2x.
x2 has an inverse, namely sqrt(x), for x>0.
The first two statements are about the square of a particular but unspecified number. In those statements "x" represents an arbitrary single thing, and x2 represents a single related thing.
The third and fourth statement are about x2, the function. But this is not clearly communicated in 3. and 4. except by context and shared understanding of Calculus. We need a notational device to distinguish between x2 the single (but arbitrary) value, and x2 the function. The device also needs to communicate that it is a function of x. Therefore lambda is invented:
"lambda x.x2" is written to make this distinction and communicate "the function of x whose value at x is x2". The lambda operator takes a name (x) and an expression (x2) and returns a function. This has the same consequences as normal function definition, except that the function doesn't automatically receive a name.

Categories