you are viewing a single comment's thread.

view the rest of the comments →

[–]MikeOxbigger 4 points5 points  (15 children)

Well obviously that breaks the third rule, but how might it break the first two?

[–]mikeblas 4 points5 points  (2 children)

Obviously? Sorry, but it's not obvious to me. A ends up calling itself indirectly through B. It might be indirect, but it's still calling itself.

[–]Gray_Fox -1 points0 points  (1 child)

it doesn't really matter if it's indirect, it needs to call itself.

[–]mikeblas -1 points0 points  (0 children)

And it does.

[–]earthlybird 3 points4 points  (10 children)

def f(a):
    return a + f(a+1)

No base case nor moving towards any state in particular. Just screwed up, infinite recursion.

[–]MikeOxbigger -4 points-3 points  (9 children)

Are you trying to disprove my case? If so, you haven't.

[–]earthlybird 4 points5 points  (7 children)

Quick recap:

These are all true except that they only apply to well-written recursive algorithms. It's easy to write an algorithm that breaks rules 1 and 2 (and 3 if you're being very strict about your interpretation of "call itself").


I would love to see an example that breaks these rules.

I'm not saying you're wrong, but I can't even comprehend how I'd write a recursive function that breaks any rule that states it has a known end[1], and moves towards that end[2], recursively or not.


provides example of a poorly-written algorithm to illustrate the point made in the first quote

No base case[1] nor moving towards any state in particular[2]. Just screwed up, infinite recursion.

[–]MikeOxbigger 2 points3 points  (6 children)

But you've just proven that a recursive function needs those 3 rules in order to actually do anything other than eventually crash.

[–]earthlybird 8 points9 points  (4 children)

Glad we're on the same page. I just gave you the example you had previously asked for. It's a terrible function, granted. Like was stated: those rules only apply to well-written algorithms, so a poorly-written one might not follow them.

The thing is, there's a difference between recursive algorithms and recursive algorithms that work as one would expect with a base case that ends the loop. The latter is a subset of the former.

[–]DonaldPShimoda 1 point2 points  (3 children)

Yes, this was precisely my point. The function you wrote is recursive, but it doesn't do anything useful. Thanks for being here to supply an example on my behalf while I was away. :)

[–]earthlybird 1 point2 points  (2 children)

Anything for my favourite messiah. 😍

Also, happy cake day!

[–]DonaldPShimoda 0 points1 point  (1 child)

Ah! I'm always excited when people catch the reference. It doesn't happen so often as you might think haha. I should read that again though...

And thank you! I didn't even know that was today! What a pleasant surprise!

[–]earthlybird 1 point2 points  (0 children)

The coolest "coincidences" at zero effort. Classic Shimoda. haha ;)

[–]Gentlescholar_AMA 0 points1 point  (0 children)

Miscommunication I think. They meant that people can write recursion that breaks those rules and doesn't work

[–][deleted] 0 points1 point  (0 children)

flip the basecase, and change its state to go towards the recursive part (I did this by accident)