V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2014-12-04 04:35:12 +08:00 · 3006 次点击
    这是一个创建于 3423 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2014-12-05 00:10:20 +08:00
    whalegia
        1
    whalegia  
       2014-12-04 05:49:55 +08:00
    为什么我把 Lambda x:x 换成 myprint 也不行……
    whalegia
        2
    whalegia  
       2014-12-04 06:51:44 +08:00
    我觉得啊,我觉得,这个其实没问题,阶乘其实算出来了,只不过没有传出来。
    你试试设置一个全局变量,然后在 n==0 的情况下把计算结果赋值给全局变量,答案就出来了。。。
    11
        3
    11  
       2014-12-04 07:30:46 +08:00   ❤️ 1
    @whalegia 别闹。。楼主在说 CPS。。

    问题在于你 k(1) 没返回啊。。 return k(1) 就行了
    ffffwh
        4
    ffffwh  
    OP
       2014-12-04 07:37:10 +08:00
    @11
    就是这个..
    whalegia
        5
    whalegia  
       2014-12-04 08:31:48 +08:00
    @11 对呀我就是这个意思啊。。。TAT
    segmentfault
        6
    segmentfault  
       2014-12-04 11:11:14 +08:00
    不来我站提问 (๑°3°๑) sf.gg
    liprais
        7
    liprais  
       2014-12-04 12:01:30 +08:00
    @segmentfault 这种无耻倒流量的方式真的很讨厌
    luoyou1014
        8
    luoyou1014  
       2014-12-04 12:30:48 +08:00
    @liprais 这有什么无耻的。
    233
        9
    233  
       2014-12-05 00:10:20 +08:00
    @segmentfault 哈哈哈哈,别卖萌
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:23 · PVG 13:23 · LAX 22:23 · JFK 01:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.