Are there any example of Mutual recursion?

前端 未结 8 788
迷失自我
迷失自我 2021-02-04 09:48

Are there any examples for a recursive function that calls an other function which calls the first one too ?

Example :

function1()
{    
    //do something         


        
相关标签:
8条回答
  • 2021-02-04 10:17

    An example might be the minmax algorithm commonly used in game programs such as chess. Starting at the top of the game tree, the goal is to find the maximum value of all the nodes at the level below, whose values are defined as the minimum of the values of the nodes below that, whose values are defines as the maximum of the values below that, whose values ...

    0 讨论(0)
  • 2021-02-04 10:22

    Mutual recursion is common in code that parses mathematical expressions (and other grammars). A recursive descent parser based on the grammar below will naturally contain mutual recursion: expression-terms-term-factor-primary-expression.

    expression
        + terms
        - terms
        terms
    
    terms
        term + terms
        term - terms
    
    term
        factor
        factor * term
        factor / term
    
    factor
        primary
        primary ^ factor
    
    primary
        ( expression )
        number
        name
        name ( expression )
    
    0 讨论(0)
提交回复
热议问题