Are there any example of Mutual recursion?

前端 未结 8 1020
迷失自我
迷失自我 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 09:58

    The proper term for this is Mutual Recursion.

    http://en.wikipedia.org/wiki/Mutual_recursion

    There's an example on that page, I'll reproduce here in Java:

    boolean even( int number )
    {    
        if( number == 0 )
            return true;
        else
            return odd(abs(number)-1)
    }
    
    boolean odd( int number )
    {
        if( number == 0 )
            return false;
        else
            return even(abs(number)-1);
    }
    

    Where abs( n ) means return the absolute value of a number.

    Clearly this is not efficient, just to demonstrate a point.

提交回复
热议问题