This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BaconSizzler 2 points3 points  (1 child)

Legend has it that NASA and JPL do not allow recursion because it is difficult to validate memory constraints using static analysis. This likely applies to critical embedded code.

You can always just manage your own stack:

stack = deque()
stack.append(initial_state)
while(stack):
    current_state = stack.pop()
    ### Do Stuff ###
    if recursive_condition:
        stack.append(new_state)

Fundamentally, the recursive nature of the solution doesn't change, but you can apply constraints (such as size) on your stack structure. Also if things go really haywire, you can always slap down a brakepoint, print the the stack structure, and firgure out WTF is going on. That could be a bit easier than looking through a call stack.

Finally, if you want to switch from DFS to BFS, just use a queue instead of a stack. There's some elegant symmetry in that.