recursion order question

L

lig

hi ,
i am trying to do something that is not working (cuz i tried that)
so i hope for some ideas about it.

ok, so i have this recursive function, lets call it recFun
and i wanted to be able to call it in a specific order.

in a simple way : evaluating some expression in its original order

see the following part of code:

class retType
{
....
retType doSomething(someType4,retType);
....
};

retType recFun(someType3 ,someType1)
{
....
someType1 b,d;
someType2 c;
someType3 a;
....
return recFun(a,b).doSomething(c,recFun(a,c))
....
}

well , i was sure (for some weird reason...) that the first call
will be recFun(a,b) but actually the first call was recFun(a,c) .

the problem is that i wanted a specific order (first first
then the second .. obviously..) and it wont do it.

some of my limitations are: i dont want to keep any copies
of the results of the calls ( it creates more nodes in some tree ...
which obviously i dont want to count)
and the second is that i need the keep the order due some
other dependencies .

so any ideas/revelations will be most welcomed .

bye
lig

p.s.
this is my first post on this kind of groups so excuse me if
i dont meet the protocol rules ...
 
J

John Harrison

lig said:
hi ,
i am trying to do something that is not working (cuz i tried that)
so i hope for some ideas about it.

ok, so i have this recursive function, lets call it recFun
and i wanted to be able to call it in a specific order.

in a simple way : evaluating some expression in its original order

see the following part of code:

class retType
{
...
retType doSomething(someType4,retType);
...
};

retType recFun(someType3 ,someType1)
{
...
someType1 b,d;
someType2 c;
someType3 a;
...
return recFun(a,b).doSomething(c,recFun(a,c))
...
}

well , i was sure (for some weird reason...) that the first call
will be recFun(a,b) but actually the first call was recFun(a,c) .

The order is undefined, a compiler could do either, and both would be right.
the problem is that i wanted a specific order (first first
then the second .. obviously..) and it wont do it.

some of my limitations are: i dont want to keep any copies
of the results of the calls ( it creates more nodes in some tree ...
which obviously i dont want to count)

Maybe you can use a reference

const retType& tmp = recFun(a,b);
return tmp.doSomething(c,recFun(a,c));

but you would have to change the function signature to be const.
and the second is that i need the keep the order due some
other dependencies .

I'd seriously look at relaxing that restriction.
so any ideas/revelations will be most welcomed .

bye
lig

john
 
H

Howard

lig said:
hi ,
i am trying to do something that is not working (cuz i tried that)
so i hope for some ideas about it.

ok, so i have this recursive function, lets call it recFun
and i wanted to be able to call it in a specific order.

in a simple way : evaluating some expression in its original order

see the following part of code:

class retType
{
...
retType doSomething(someType4,retType);
...
};

retType recFun(someType3 ,someType1)

Are someType3 and someType1 classes? Are they defined somewhere? Why don't
you have names for them here? Aren't you planning on using them inside the
function somewhere?

(Also, are you sure you want to pass those by value?)
{
...
someType1 b,d;
someType2 c;
someType3 a;

These are local variables. What about the parameters that were passed in.
As it is, you're ignoring them entirely.
...
return recFun(a,b).doSomething(c,recFun(a,c))

You've said above that c is of the type "someType2", but you're passing as
the first parameter to doSomething, which expects a variable of type
"someType4". You're also passing it as the second parameter of recFun,
which is expecting a "someType1" variable.
...
}

well , i was sure (for some weird reason...) that the first call
will be recFun(a,b) but actually the first call was recFun(a,c) .

How else could it work? It has to pass a retType variable to doSomething.
In order to do that, it first needs to call recFun, in order to get back the
retType result, right? So, before it can call doSomething, it has to
execute recFun.
the problem is that i wanted a specific order (first first
then the second .. obviously..) and it wont do it.

some of my limitations are: i dont want to keep any copies
of the results of the calls ( it creates more nodes in some tree ...
which obviously i dont want to count)

If you don't want copies, then you probably don't want to pass and return by
value. But what you _do_ want is really impossible for me to tell, because
your code doesn't really make sense.

(And how does doSomething create a retType object. You left out that code.)
and the second is that i need the keep the order due some
other dependencies .

I'm not sure what order it is you need to keep. But there's no way to
execute doSomething unless you give it the values it needs, which means you
have to somehow compute those values first (unless doSomething itself can
make a function call to get the value it needs, instead of passing it that
second parameter).

-Howard
 
H

Howard

Howard said:
How else could it work? It has to pass a retType variable to doSomething.
In order to do that, it first needs to call recFun, in order to get back
the retType result, right? So, before it can call doSomething, it has to
execute recFun.

Oops, my mistake. You were talking about the two calls to recFun, not the
call to doSomething. As John stated, the order of those calls is not
defined. But you can put them in separate statements like he showed to
control the order.

-Howard
 
J

John Harrison

Jack said:
lig wrote:

[snip]

return recFun(a,b).doSomething(c,recFun(a,c))
...
}

well , i was sure (for some weird reason...) that the first call
will be recFun(a,b) but actually the first call was recFun(a,c) .

The order is undefined, a compiler could do either, and both would be right.


ITYM "unspecified", not "undefined". A world of difference.

Right, in other words it is one order or the other.

john
 
L

lig

first thanks for the ideas .

second , yea i did a type mistake there above ... (tried to keep it
simple)

i'll think i'll try the local copy option. i missed the part that the
dtor of
retType class (which i didnt wrote myself) is removing his projection
from the tree , so the limitation of not keeping copies is no longer
relevant.

thanks again for your ideas.

lig
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top