利用lambda和条件表达式构造匿名递归函数

from operator import sub, mul                                             
 
def make_anonymous_factorial():
    """Return the value of an expression that computes factorial.

    >>> make_anonymous_factorial()(5)
    120
    """
    return (lambda f: lambda n: f(f, n))(lambda f, n: 1 if n == 1 else mul(n, f(f, sub(n, 1)))) 

posted @ 2019-03-03 16:32  飞飞翔滴少年  阅读(409)  评论(0编辑  收藏  举报