推荐学习书目
› 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
whereFly
V2EX  ›  Python

请问这算是递归函数码?

  •  
  •   whereFly · Jun 10, 2022 · 4223 views
    This topic created in 1571 days ago, the information mentioned may be changed or developed.
    def aa1(n): if n == 1: return 1 else: aa2(n)

    def aa2(n): m = n-1 aa1(m)

    14 replies  •  2022-06-11 15:39:25 +08:00
    ws52001
        1
    ws52001  
       Jun 10, 2022
    算的,递归函数的定义就是直接或者间接调用函数本身。你这个算是间接调用。
    Jooooooooo
        2
    Jooooooooo  
       Jun 10, 2022
    算, 你把 aa2 展开放到 aa1 里就更直观了.
    ychost
        3
    ychost  
       Jun 10, 2022
    这是循环依赖,不过也算递归
    LLaMA2
        4
    LLaMA2  
       Jun 10, 2022
    ```
    function aa1(n: number) {
    while (n === 1) {
    return 1;
    }
    aa1(n - 1);
    }
    ```

    你写的不就是这个
    breeza
        5
    breeza  
       Jun 10, 2022
    这个叫互递归
    hello2090
        6
    hello2090  
       Jun 10, 2022 via iPhone
    你这不就 aa1(n - 1)么
    whereFly
        7
    whereFly  
    OP
       Jun 10, 2022
    @ws52001 递归函数+time.sleep 间隔一段时间循环执行会遇到栈溢出,无解了吗?
    IMXT
        8
    IMXT  
       Jun 10, 2022 via Android
    尾递归就不会爆栈了,前提是编译器实现了尾递归优化
    necomancer
        9
    necomancer  
       Jun 10, 2022
    是不是少了 return ?(在 else 和 aa2 里)
    dingwen07
        10
    dingwen07  
       Jun 11, 2022 via iPhone
    @whereFly #7
    Python 无解吧,不会对 tail recursion 优化
    几乎所有递归都可以改循环
    xiayushengfan
        11
    xiayushengfan  
       Jun 11, 2022
    禁止套娃
    anshigm
        12
    anshigm  
       Jun 11, 2022
    逻辑上不就是这样?
    while (n !=1 )
    n -=1
    return 1
    一个简单的循环能搞定为什么要用这么复杂的递归?这么写有什么深意吗?
    whereFly
        13
    whereFly  
    OP
       Jun 11, 2022
    @anshigm 只是举例,实际是用了递归函数+time.sleep 来间隔一定时间执行程序,然后就遇到了栈溢出。
    aijam
        14
    aijam  
       Jun 11, 2022
    mutual recursion
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   957 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 22:42 · PVG 06:42 · LAX 15:42 · JFK 18:42
    ♥ Do have faith in what you're doing.