By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,377 Members | 3,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,377 IT Pros & Developers. It's quick & easy.

Example of 2 mutually recursive functions

P: n/a
Please give an example of 2 simple mutually recursive functions in C .

Apr 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ico
jeniffer <ze******************@gmail.com> wrote:
Please give an example of 2 simple mutually recursive functions in C .


Why ?

--
:wq
^X^Cy^K^X^C^C^C^C
Apr 13 '06 #2

P: n/a
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
es*****@acm-dot-org.invalid
Apr 13 '06 #3

P: n/a
"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. .
Apr 13 '06 #4

P: n/a

"osmium" <r1********@comcast.net> wrote in message
news:4a************@individual.net...
"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...
Apr 13 '06 #5

P: n/a
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)
Apr 13 '06 #6

P: n/a
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,U a,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
Apr 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.