Example of 2 mutually recursive functions

E

Eric Sosman

jeniffer said:
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.
 
O

osmium

jeniffer said:
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. .
 
D

David Wade

osmium said:
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...
 
R

Richard Heathfield

osmium said:
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 ...
}
}
 
P

pete

jeniffer said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Functions 2
C Programming functions 2
Scss functions and returns 0
Mutually-Recursive Functions 13
The Towers of Hanoi 3
Need help with finding N. 1
Simple functions don't work :( 3
Webview Javascript functions? 1

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top