DEV Community

Mayemene Fomene Jean Vladimir
Mayemene Fomene Jean Vladimir

Posted on

Make A Recursive Anonymous Factorial Function In Python.

I'm currently taking Structure and Interpretation of computer programs from the University of Berkeley. Here is a nice question from one of the homeworks. You might want to try your hands at it.

Q6: Anonymous factorial
The recursive factorial function can be written as a single expression by using a conditional expression.

>>> fact = lambda n: 1 if n == 1 else mul(n, fact(sub(n, 1)))
>>> fact(5)
120
Enter fullscreen mode Exit fullscreen mode

However, this implementation relies on the fact (no pun intended) that fact has a name, to which we refer in the body of fact. To write a recursive function, we have always given it a name using a def or assignment statement so that we can refer to the function within its own body. In this question, your job is to define fact recursively without giving it a name!

Write an expression that computes n factorial using only call expressions, conditional expressions, and lambda expressions (no assignment or def statements). Note in particular that you are not allowed to use make_anonymous_factorial in your return expression. The sub and mul functions from the operator module are the only built-in functions required to solve this problem:

from operator import sub, mul

def make_anonymous_factorial():
"""Return the value of an expression that computes factorial.

>>> make_anonymous_factorial()(5)
120
>>> from construct_check import check
>>> check(HW_SOURCE_FILE, 'make_anonymous_factorial', ['Assign', 'AugAssign', 'FunctionDef', 'Recursion'])
True
"""
return 'YOUR_EXPRESSION_HERE'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)