Valhalla Legends Archive

Programming => General Programming => Topic started by: Imperceptus on October 22, 2007, 07:40 PM

Title: Recursive
Post by: Imperceptus on October 22, 2007, 07:40 PM
When using recursive functions, how can you exit the function to return to the parent procedure that called it?


some_function()
  if somevar != apples call some_function

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?
Title: Re: Recursive
Post by: Banana fanna fo fanna on October 22, 2007, 07:44 PM
You need to specify an exit condition, a branch that proceeds that doesn't recurse.:

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)


Very inefficient implementation btw.
Title: Re: Recursive
Post by: squiggly on October 22, 2007, 07:46 PM
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
Title: Re: Recursive
Post by: Imperceptus on October 22, 2007, 08:25 PM
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.
Title: Re: Recursive
Post by: squiggly on October 22, 2007, 08:28 PM
Quote from: Imperceptus on October 22, 2007, 08:25 PM
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.

I guess you could do that, but it would be really very messy
Title: Re: Recursive
Post by: brew on October 22, 2007, 08:28 PM
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.)
Title: Re: Recursive
Post by: Yegg on October 22, 2007, 08:37 PM
Quote from: brew on October 22, 2007, 08:28 PM
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.)

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.
Title: Re: Recursive
Post by: squiggly on October 22, 2007, 08:51 PM
Recursive functions make you cooler, and more dangerous


Chea
Title: Re: Recursive
Post by: Yegg on October 22, 2007, 09:02 PM
Quote from: squiggly on October 22, 2007, 08:51 PM
Recursive functions make you cooler, and more dangerous


Chea

That's the smartest thing I've ever heard anyone say about anything.
Title: Re: Recursive
Post by: Imperceptus on October 22, 2007, 09:20 PM
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.
Title: Re: Recursive
Post by: K on October 22, 2007, 09:32 PM
This is a very common problem when working with graphs.  Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra's_algorithm) will solve this problem for you.

Title: Re: Recursive
Post by: Banana fanna fo fanna on October 22, 2007, 11:23 PM
Quote from: K on October 22, 2007, 09:32 PM
This is a very common problem when working with graphs.  Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra's_algorithm) will solve this problem for you.



QFT
Title: Re: Recursive
Post by: Noodlez on October 28, 2007, 07:16 PM
T.T