Example of 2 mutually recursive functions

Discussion in 'C Programming' started by jeniffer, Apr 13, 2006.

  1. jeniffer

    jeniffer Guest

    Please give an example of 2 simple mutually recursive functions in C .
     
    jeniffer, Apr 13, 2006
    #1
    1. Advertising

  2. jeniffer

    Ico Guest

    jeniffer <> wrote:
    > Please give an example of 2 simple mutually recursive functions in C .


    Why ?

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, Apr 13, 2006
    #2
    1. Advertising

  3. jeniffer

    Eric Sosman Guest

    jeniffer wrote:
    > Please give an example of 2 simple mutually recursive functions in C .


    int moan(void);

    int main(void) {
    return moan();
    }

    int moan(void) {
    return main();
    }

    You're welcome.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 13, 2006
    #3
  4. jeniffer

    osmium Guest

    "jeniffer" writes:

    > Please give an example of 2 simple mutually recursive functions in C .


    IMO that's a toughie. Homey, believable examples are hard to come by. I
    think your best bet is to look for its use in parsing. If you poke around a
    while you could probably come up with something that would make sense to an
    ordinary person. I know I captured an example a year or so ago but I have
    no idea how to find it in my voluminous files, I don't have a real OS, I use
    Windows. .
     
    osmium, Apr 13, 2006
    #4
  5. jeniffer

    David Wade Guest

    "osmium" <> wrote in message
    news:...
    > "jeniffer" writes:
    >
    > > Please give an example of 2 simple mutually recursive functions in C .

    >
    > IMO that's a toughie. Homey, believable examples are hard to come by. I
    > think your best bet is to look for its use in parsing. If you poke around

    a
    > while you could probably come up with something that would make sense to

    an
    > ordinary person. I know I captured an example a year or so ago but I have
    > no idea how to find it in my voluminous files, I don't have a real OS, I

    use
    > Windows. .
    >
    >


    some times "asin" and "acos" are implemented in this way...
     
    David Wade, Apr 13, 2006
    #5
  6. osmium said:

    > "jeniffer" writes:
    >
    >> Please give an example of 2 simple mutually recursive functions in C .

    >
    > IMO that's a toughie. Homey, believable examples are hard to come by. I
    > think your best bet is to look for its use in parsing. If you poke around
    > a while you could probably come up with something that would make sense to
    > an
    > ordinary person. I know I captured an example a year or so ago but I have
    > no idea how to find it in my voluminous files, I don't have a real OS, I
    > use
    > Windows.


    Poor chap, there there, etc. :-(

    Anyway, one obvious example for you, which should be clear as daylight to
    anyone who studied mathematics up to the age of about ten or eleven:

    void bn_add(bignum **result, bignum *x, bignum *y)
    {
    if(bn_lt(x, 0))
    {
    x->sign = 1;
    bn_subtract(result, y, x);
    x->sign = -1;
    }
    else if(bn_lt(y, 0))
    {
    y->sign = 1;
    bn_subtract(result, x, y);
    y->sign = -1;
    }
    else
    {
    ... get on with the addition ...
    }
    }

    void bn_subtract(bignum **result, bignum *x, bignum *y)
    {
    if(bn_lt(y, 0))
    {
    y->sign = 1;
    bn_add(result, x, y);
    y->sign = -1;
    }
    else if(bn_lt(x, 0))
    {
    x->sign = 1;
    bn_add(result, x, y);
    x->sign = -1;
    (*result)->sign = -(*result)->sign;
    }
    else
    {
    ...get on with the subtraction ...
    }
    }


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 13, 2006
    #6
  7. jeniffer

    pete Guest

    jeniffer wrote:
    >
    > Please give an example of 2 simple mutually recursive functions in C .


    Here's one:

    /******************************************************************/

    #define F(Q,R,P) Q(int x){int i=x;while(i--)x=R(x,x);return x;}\
    P(int L,int x){int i=x;if(L--)while(i--)x=P(L,x);return Q(x);}

    #define Y(A,z,B,C,D,E,G,H,I,J,K,M,N,O,S,T,U,V,W)\
    F(A,z,B)F(C,B,D)F(E,D,G)F(H,G,I)F(J,I,K)F(M,K,N)F(O,N,S)F(T,S,U)F(V,U,W)

    Z(int L,int x)
    {
    int i = x;

    if(L--)
    while(i--)
    x = Z(L,x);
    return x << x;
    }

    Y(a,Z,b,c,d,e,g,h,X,j,k,m,n,o,s,t,u,v,w)
    Y(Aa,w,Ba,Ca,Da,Ea,Ga,Ha,Ia,Ja,Ka,Ma,Na,Oa,Sa,Ta,Ua,Va,Wa)
    Y(Ab,Wa,Bb,Cb,Db,Eb,Gb,Hb,Ib,Jb,Kb,V,U,W,T,S,O,N,M)
    F(A,M,B)
    F(C,B,D)
    F(E,D,G)
    F(H,G,I)
    F(J,I,K)

    int main()
    {
    return K(99999,9);
    }

    /*******************************************************************/

    Here's David Moews' explanation of the code,
    which is a better one than I could give,
    except that by "outputs", he means "returns" :

    {pete-4.c} uses the following set of recursive definitions
    (generated by preprocessor abuse):

    f(0,x) := x * (2**x),
    g(m+1,0,x) := f(m,x),
    g(m+1,n+1,x) := f(m,(g(m+1,n,?)@@x)(x)),
    h(m,x) := g(m,x,x) (m > 0),
    f(m,x) := (h(m,?)@@x)(x) (m > 0).

    {pete-4.c} outputs g(33,99999,9).
    The definition of g(m,n,x) is very similar to that of F[m,n](x),
    and it is easy to prove that

    F[n+2](x) <= g(1,n,x) <= F[n+2](x+2) (x > 0),
    F[m,n+1](x) <= g(m+1,n,x) <= F[m,n+1](x+2) (m > 0, x > 0).

    so

    F[32,10**5](9) <= g(33,99999,9) <= F[32,10**5](11).


    --
    pete
     
    pete, Apr 20, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niklas Matthies
    Replies:
    0
    Views:
    839
    Niklas Matthies
    Oct 24, 2006
  2. Peter Olcott
    Replies:
    4
    Views:
    343
    Markus Schoder
    Jun 4, 2006
  3. Marco
    Replies:
    4
    Views:
    325
  4. vamsi
    Replies:
    21
    Views:
    2,081
    Keith Thompson
    Mar 9, 2009
  5. Revence Kalibwani

    Mutually-Recursive Functions

    Revence Kalibwani, Jun 7, 2007, in forum: Ruby
    Replies:
    13
    Views:
    200
    bbiker
    Jun 7, 2007
Loading...

Share This Page