推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
ffffwh
V2EX  ›  Python

这个 CPS 的阶乘哪有问题么?

  •  
  •   ffffwh · Dec 4, 2014 · 4038 views
    This topic created in 4352 days ago, the information mentioned may be changed or developed.
    def fact(k, n):
        if n == 0:
            k(1)
        else:
            return fact(lambda r: k(r * n), n - 1)
    
    >>> a = fact(lambda x: x, 10)
    

    a应该是那个10!数字的, 现在a不知道是什么。
    把lambda x: x换成myprint的话到可以。其中

    def myprint(x):
        print x
    
    9 replies  •  2014-12-05 00:10:20 +08:00
    whalegia
    whalegia
    Dec 4, 2014
    1
    为什么我把 Lambda x:x 换成 myprint 也不行……
    whalegia
    whalegia
    Dec 4, 2014
    2
    我觉得啊,我觉得,这个其实没问题,阶乘其实算出来了,只不过没有传出来。
    你试试设置一个全局变量,然后在 n==0 的情况下把计算结果赋值给全局变量,答案就出来了。。。
    11
    11
    Dec 4, 2014❤️1
    3
    @whalegia 别闹。。楼主在说 CPS。。

    问题在于你 k(1) 没返回啊。。 return k(1) 就行了
    ffffwh
    ffffwh
    OP
    Dec 4, 2014
    4
    @11
    就是这个..
    whalegia
    whalegia
    Dec 4, 2014
    5
    @11 对呀我就是这个意思啊。。。TAT
    segmentfault
    segmentfault
    Dec 4, 2014
    6
    不来我站提问 (๑°3°๑) sf.gg
    F281M6Dh8DXpD1g2
    F281M6Dh8DXpD1g2
    Dec 4, 2014
    7
    @segmentfault 这种无耻倒流量的方式真的很讨厌
    luoyou1014
    luoyou1014
    Dec 4, 2014
    8
    @liprais 这有什么无耻的。
    233
    233
    Dec 5, 2014
    9
    @segmentfault 哈哈哈哈,别卖萌
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5195 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 06:39 · PVG 14:39 · LAX 22:39 · JFK 01:39
    ♥ Do have faith in what you're doing.