推荐学习书目
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 · 3763 views
    This topic created in 4203 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
        1
    whalegia  
       Dec 4, 2014
    为什么我把 Lambda x:x 换成 myprint 也不行……
    whalegia
        2
    whalegia  
       Dec 4, 2014
    我觉得啊,我觉得,这个其实没问题,阶乘其实算出来了,只不过没有传出来。
    你试试设置一个全局变量,然后在 n==0 的情况下把计算结果赋值给全局变量,答案就出来了。。。
    11
        3
    11  
       Dec 4, 2014   ❤️ 1
    @whalegia 别闹。。楼主在说 CPS。。

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