javascript-tutorial / en.javascript.info

Modern JavaScript Tutorial
https://javascript.info
Other
23.66k stars 3.88k forks source link

Is recursive setTimeOut really recursive? #1277

Closed paroche closed 5 years ago

paroche commented 5 years ago

Maybe it's standard terminology, I don't know, but it doesn't seem to me that the examples where setTimeOut calls itself are really recursive - it looks like the functions are "respawning" themselves at the end of their execution, rather than leaving themselves pending while the nested function executes as they would in recursion (as I understand it). To rephrase, since the calling function does not "contain" the called version of itself (does it?), I wouldn't think of it as recursion, but rather as self-chaining (just making up terms here).

iliakan commented 5 years ago

From the Computer Science POV, recursion doesn't imply keeping the stack.

It's just that a function calls itself. Even a call to itself after a timeout and not returning the execution back is still a recursion.

Please correct me if I'm wrong =)

https://en.wikipedia.org/wiki/Recursion_(computer_science)

paroche commented 5 years ago

Well, that's not what I recall ever learning. And in the Wikipedia article linked to, it opens with: "Recursion in computer science is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem (as opposed to iteration).[1] The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science.[2]" In the setTimeOut examples, I don't see the calls as being "smaller instances of the same problem", as they would for tree navigation, factorials, etc.

W/ a little browsing I found this also: In a Java tutorial: Characteristics of Recursive Methods:

And in a Python tutorial:

Definition of Recursion Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition fulfils the condition of recursion, we call this function a recursive function. 

Termination condition: A recursive function has to terminate to be used in a program. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not met in the calls. 

Example:  4! = 4 3! 3! = 3 2! 2! = 2 1  Replacing the calculated values gives us the following expression  4! = 4 3 2

Generally we can say: Recursion in computer science is a method where the solution to a problem is based on solving smaller instances of the same problem.

https://www.python-course.eu/recursive_functions.php

(the above definition, unfortunately, does not actually "define" the term, but more describes what they want you to understand about it).

I do see multiple instances where recursion is initially defined as "a function that calls itself", but then they always go on to talk about the base case and breaking the problem into smaller problems. As here also:https://www.geeksforgeeks.org/recursion/

So I suppose there is some wiggle room there, and I haven't been able to find a definition for a function that calls itself but doesn't meet the other criteria usually associated with recursion. Which is funny, I would think that would be covered somewhere. But I think when a function calls itself as it is terminating, with its dying breath, so to speak, it doesn't seem to me like the concept of "recursion" really applies. It's qualitatively quite different -- it's daisy chaining itself, as contrasted with containing itself or defining itself in terms of itself. So it seems to me. But I have no definitive authority to quote in support of my case, beyond what's above

-----Original Message----- From: Ilya Kantor notifications@github.com To: javascript-tutorial/en.javascript.info en.javascript.info@noreply.github.com Cc: paroche sunmtnsft@aol.com; Author author@noreply.github.com Sent: Mon, Aug 26, 2019 11:38 pm Subject: Re: [javascript-tutorial/en.javascript.info] Is recursive setTimeOut really recursive? (#1277)

From the Computer Science POV, recursion doesn't imply keeping the stack.It's just that a function calls itself. Even a call to itself after a timeout and not returning the execution back is still a recursion.Please correct me if I'm wrong =)https://en.wikipedia.org/wiki/Recursion_(computer_science)— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

iliakan commented 5 years ago

Let's call it "nested setTimeout".

paroche commented 5 years ago

Works for me.

-----Original Message----- From: Ilya Kantor notifications@github.com To: javascript-tutorial/en.javascript.info en.javascript.info@noreply.github.com Cc: paroche sunmtnsft@aol.com; Author author@noreply.github.com Sent: Tue, Aug 27, 2019 12:35 am Subject: Re: [javascript-tutorial/en.javascript.info] Is recursive setTimeOut really recursive? (#1277)

Let's call it "nested setTimeout".— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

iliakan commented 5 years ago

I replaced what I could find.