• Welcome to Valhalla Legends Archive.
 

Recursive

Started by Imperceptus, October 22, 2007, 07:40 PM

Previous topic - Next topic

Imperceptus

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?
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

Banana fanna fo fanna

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.

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
- Posso usar um tradutor de língua, devo ser fresco agora!

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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

squiggly

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
- Posso usar um tradutor de língua, devo ser fresco agora!

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.)
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Yegg

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.

squiggly

Recursive functions make you cooler, and more dangerous


Chea
- Posso usar um tradutor de língua, devo ser fresco agora!

Yegg

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.

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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

K

This is a very common problem when working with graphs.  Dijkstra's algorithm will solve this problem for you.


Banana fanna fo fanna

Quote from: K on October 22, 2007, 09:32 PM
This is a very common problem when working with graphs.  Dijkstra's algorithm will solve this problem for you.



QFT

Noodlez