Valhalla Legends Forums Archive | General Programming | Recursive

AuthorMessageTime
Imperceptus
When using recursive functions, how can you exit the function to return to the parent procedure that called it?

[code]
some_function()
  if somevar != apples call some_function
[/code]
If that looped 5 times and then apples were found how would you exit the entire process back to what called some_function to begin with?
October 23, 2007, 12:40 AM
St0rm.iD
You need to specify an exit condition, a branch that proceeds that doesn't recurse.:
[code]
def fibo(n):
    if n == 0:
        return 0 # exit condition
    elif n == 1:
        return 1 # exit condition
    else:
        return fibo(n-1) + fibo(n-2)
[/code]

Very inefficient implementation btw.
October 23, 2007, 12:44 AM
squiggly
shouldn't your function just do that as a consequence of being done?

like, your last iteration returns None, which results in the parent procedure returning None, and so on
October 23, 2007, 12:46 AM
Imperceptus
Mainly I was reflecting on the old game Trade wars. Tons of fun, but I wanted to try and see how you could make the virtual universe that it ran off of. This involves sectors of pointing to other sectors and so forth. I made a sample to generate a really rough universe. I figured the mapping for finding the fastest path through adjacent sectors to the destination should use a recursive function(It worked in my mind).  After testing this im a bit lost to how to do this.
October 23, 2007, 1:25 AM
squiggly
[quote author=Imperceptus link=topic=17129.msg174116#msg174116 date=1193102740]
Mainly I was reflecting on the old game Trade wars. Tons of fun, but I wanted to try and see how you could make the virtual universe that it ran off of. This involves sectors of pointing to other sectors and so forth. I made a sample to generate a really rough universe. I figured the mapping for finding the fastest path through adjacent sectors to the destination should use a recursive function(It worked in my mind).  After testing this im a bit lost to how to do this.
[/quote]

I guess you could do that, but it would be really very messy
October 23, 2007, 1:28 AM
BreW
You should never have to use recursive functions. Ever. It's bad programming practice. Plus, you're pwning stack.
Save that kinda stuff for functional shit like LISP. (heh. yegg is going to have fun posting in this topic.)
October 23, 2007, 1:28 AM
Yegg
[quote author=brew link=topic=17129.msg174118#msg174118 date=1193102906]
You should never have to use recursive functions. Ever. It's bad programming practice. Plus, you're pwning stack.
Save that kinda stuff for functional shit like LISP. (heh. yegg is going to have fun posting in this topic.)
[/quote]

In languages that allow you to create cleaner and more "to the point" code, recursion is a very useful tool. I'm sure banana fanna fo fanna would also agree.
October 23, 2007, 1:37 AM
squiggly
Recursive functions make you cooler, and more dangerous


Chea
October 23, 2007, 1:51 AM
Yegg
[quote author=squiggly link=topic=17129.msg174123#msg174123 date=1193104301]
Recursive functions make you cooler, and more dangerous


Chea
[/quote]

That's the smartest thing I've ever heard anyone say about anything.
October 23, 2007, 2:02 AM
Imperceptus
I have found recursion really good in projects that involve heirarchy or however thats spelled. Guess I will have to think of a better way to find a path.  If anyone has an idea, pls pm me(or mod split this topic with replies). dont wanna have a scrambled topic.

And yes Squiggly, very VERY messy. If you figure every sector has 5 possible adjacent sectors that it can go to to find a possible shortest path.
October 23, 2007, 2:20 AM
K
This is a very common problem when working with graphs.  Dijkstra's algorithm will solve this problem for you.

October 23, 2007, 2:32 AM
St0rm.iD
[quote author=K link=topic=17129.msg174129#msg174129 date=1193106728]
This is a very common problem when working with graphs.  Dijkstra's algorithm will solve this problem for you.


[/quote]

QFT
October 23, 2007, 4:23 AM
Noodlez
T.T
October 29, 2007, 12:16 AM

Search