473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

recursion order question

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 ...

Sep 15 '05 #1
6 1425
lig wrote:
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
Sep 15 '05 #2

"lig" <ar*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
Sep 15 '05 #3

"Howard" <al*****@hotmail.com> wrote in message
news:7LiWe.45855$qY1.21373@bgtnsc04-

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.


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
Sep 15 '05 #4
On Thu, 15 Sep 2005 18:01:18 GMT, John Harrison
<jo*************@hotmail.com> wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 16 '05 #5
Jack Klein wrote:
On Thu, 15 Sep 2005 18:01:18 GMT, John Harrison
<jo*************@hotmail.com> wrote in comp.lang.c++:

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
Sep 16 '05 #6
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

Sep 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
4
by: chandra.somesh | last post by:
I recently was having trouble converting implementaion of recursions using stack. While single level of recursions are quite intuitive , it is when we have more than one recursive calls in the...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
2
by: jw | last post by:
what is the relation between a stack and recursion func. and if i have recursion funct(a return type void), void print(int n,int base){ static string digits="0123456789abcdef"; if(n>=base){...
5
by: red_hax0r | last post by:
I'm trying to learn how to make a scrambling algorithm in C that will turn out all possible permutations of a word without repeating (but repeating can easily be fixed). I found out a few...
3
by: mj | last post by:
I am a newbie to C++ but not to programming. I am proficient in VB 6.0 and have some skills in VB.net. Recursion is a new idea for me and being from the VB world I rested heavily on stepping...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.