Live data from Hacker News

Understanding Recursion as an Absolute Beginner

bigomega.dev

11–20 of 55 posts

Re: Understanding Recursion as an Absolute Beginner

#11
post #2

countDownFrom(--n) There's no reason to reassign the decremented n within the function. Prefer: countDownFrom(n - 1)

Thank you! This immediately stood out to me as a clever hack that makes it more complicated than it needs to be. I'd have written this function as:

    function countDownFrom(n) {
      console.log(n)
    
      if (n > 0) {
        countDownFrom(n - 1)
      }
    }
This removes both the decrement operator and the return keyword, both of which distract from the concept being taught.

This would also make it simple to add a `step` argument, which could be written as:

    function countDownFrom (n, step) {
      console.log(n)
    
      if (n > 0) {
        countDownFrom(n - step, step)
      }
    }

Re: Understanding Recursion as an Absolute Beginner

#13

Is there a good example of a time where recursion would be much more efficient than iteration?

Recursion is efficient in developer time: it saves you from having to keep track of context yourself because you can just stick it on the call stack.

Re: Understanding Recursion as an Absolute Beginner

#14
post #2

countDownFrom(--n) There's no reason to reassign the decremented n within the function. Prefer: countDownFrom(n - 1)

Thank you! This immediately stood out to me as a clever hack that makes it more complicated than it needs to be. I'd have written this function as: function countDownFrom(n) { console.log(n) if (n > 0) { countDownFrom(n - 1) } } This removes both the decrement operator and the return keyword, both of which distract from the concept being taught. This would also make it simple to add a `step` argument, which could be…

coutDownfrom(n--) contains a pointless assignment to a variable that has no use in the function after the logging call. It's not what I'd call clever.

It spoils the teaching value of the solution, because a beginner might be confused into thinking that the mutation of n is essential, like that there is only a single n and it is being stepped to bring about the countdown.

The student needs to understand that the countdown occurs even though there are no assignments to n, nor anything imperative other than the side effect of the logging call.

Don't write beginner examples which contain red herrings that must be explained away to some of the beginners.

Re: Understanding Recursion as an Absolute Beginner

#15
I think recursion is very much about finding the right application to learn it, rather than trying to shoehorn an example that doesn't wind up needing a recursive solution.

I wound up needing to learn recursion very early on in my programming journey because the problem space I was working in at the time (3D computer graphics) needed it. I found it relatively easy to reason about recursion because the solutions became simpler, more efficient, and more effective when they were solved recursively. Finding those problems that clearly need recursion, are simple enough to grasp, and make good sense are hard, not recursion itself.

The variety of contexts that beginners have and the gravitation of computer science to tree traversals as a standard example (or a contrived one like this) is why I think so many get tripped up on recursion when it's introduced.

Re: Understanding Recursion as an Absolute Beginner

#16
I think the simplest way to understand recursion is as a for loop where you use the stack as the counter.

Recursion is basically implementing the operations of a repetitive loop but the loop controls are not explicit like in a for loop, instead you use the stack as the counter of the loop.

Once you think of recursion as just another way to do for loops, it immediately is demystified.

Re: Understanding Recursion as an Absolute Beginner

#18

Is there a good example of a time where recursion would be much more efficient than iteration?

I'm doubtful that recursion would ever be more performant than iteration. Iteration with a stack gives you all the capability of recursion without the function call overhead. As others have said, the main advantage of recursion is ease of designing and understanding the solution.

Re: Understanding Recursion as an Absolute Beginner

#19

>Most of the people find recursion difficult but it's not. Recursion is really hard. According to https://phys.org/news/2019-08-recursive-language-modern-simu... Recursive language is a relatively recent phenomenon and even now is much harder to acquire than language and grammar. Recursion is probably the biggest thing that separates our communication from that of other species and gives such a big advantage. That be…

Everyone already understands recursion, they just dont know it. If you learned to count, you learned recursion.

Re: Understanding Recursion as an Absolute Beginner

#20

> To do that we can simply return without returning anything i.e. null. I'm no JavaScript expert, but a function that returns nothing results in undefined, no?

Indeed, null!==undefined and if you want to return null you must return null.

However, null==undefined so you might not even notice the difference.

Post reply on HN