推荐学习书目
› 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 · 4352 views
    This topic created in 1611 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
    ws52001
    Jun 10, 2022
    1
    算的,递归函数的定义就是直接或者间接调用函数本身。你这个算是间接调用。
    Jooooooooo
    Jooooooooo
    Jun 10, 2022
    2
    算, 你把 aa2 展开放到 aa1 里就更直观了.
    ychost
    ychost
    Jun 10, 2022
    3
    这是循环依赖,不过也算递归
    LLaMA2
    LLaMA2
    Jun 10, 2022
    4
    ```
    function aa1(n: number) {
    while (n === 1) {
    return 1;
    }
    aa1(n - 1);
    }
    ```

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