Live data from Hacker News

How not to teach recursion (2021)

parentheticallyspeaking.org

101–110 of 121 posts

Re: How not to teach recursion (2021)

#101
post #99
post #95

Earlier quoted context omitted.

That would require OS specific APIs which are distracting.

Quoted post unavailable.

Python has other issues which make it less than ideal for a learning language. Really it's having to deal with an API at all that's distracting anyway.

Re: How not to teach recursion (2021)

#102
post #92
post #86

Earlier quoted context omitted.

It was counterproductive for me when recursion was taught with Fibonacci and factorials. They were literally the opposite of easy to understand for me, I had absolutely no idea why those examples actually worked, so I just gave up and treated them like magic. I think recursion should be taught with examples that would be easier for a student to write with recursion than without it. For example, listing all files in a…

Why would you think enumerating a directory is not magic? Walking a tree / BFS is quite easy to implement iteratively, using a search queue. It's a common example for teaching Lisp. Recursion is most natural in problems like implementing evaluation of an abstract syntax trer: eval(tree) = apply(node(tree), map(eval, leaves(tree)) )

And since a tree is a graph, you can just use adjacency matrix and two loops. :)

O(n^2) but very simple.

Re: How not to teach recursion (2021)

#103
post #52

I see nothing wrong with either factorial or Fibonacci numbers. The author argues that nobody uses or needs neither factorial nor Fibonacci numbers. But that is not the point. If you are going to teach a concept it's best to use easy to understand examples. And factorial Fibonacci numbers are just that. I rather use factorial to teach recursion than QuickSort using Hoare partition algorithm. And if it comes about tea…

i think the problem is that most people try to follow the recursions in their head which is hard. the biggest lesson regarding recursion is that if you're lucky enough to have your problem fit with tco (or have guarantees your problem is small enough), it's actually way simpler to both write and verify. write the base case, write the inductive step, translate to code, done. no hard reasoning about the code required b…

>i think the problem is that most people try to follow the recursions in their head which is hard.

It's hard for computers, too as it can easy result in stack overflow if there are too many steps.

Re: How not to teach recursion (2021)

#104

I see nothing wrong with either factorial or Fibonacci numbers. The author argues that nobody uses or needs neither factorial nor Fibonacci numbers. But that is not the point. If you are going to teach a concept it's best to use easy to understand examples. And factorial Fibonacci numbers are just that. I rather use factorial to teach recursion than QuickSort using Hoare partition algorithm. And if it comes about tea…

This might be the root of the problem for the author: >>> In principle, natural numbers are also recursive data: a number is either zero or the successor of another natural number. However, this way of thinking is not natural to students... That's because students didn't learn math. "Back in my day," we learned mathematical induction sometime in middle school, and were re-introduced to it from time to time as a style…

Most of the time a sequence of numbers is recursively defined. Ex. Xn = Xn-1 + 2n

So a student might be familiar with it.

Re: How not to teach recursion (2021)

#105
My go to has always been Line Drawing. If the line is Not the best or efficient line drawing method, but you can immediately use it for stuff once it's made, and using something that you understand how it works counts for a lot when you're learning.

Re: How not to teach recursion (2021)

#106
post #70

> For instance, even a child informed about biology can answer basic questions like these: - Does a child have (biological) parents? - Yes. - How many? - Two, a (biological) male and (biological) female. [...] They can similarly see other kinds of self-referential data even at a young age, well before they program. Isn’t this a horrific example for recursion? Wouldn’t most adults realise that the structure isn’t a tr…

How is a family TREE not a tree

Re: How not to teach recursion (2021)

#108
post #70

> For instance, even a child informed about biology can answer basic questions like these: - Does a child have (biological) parents? - Yes. - How many? - Two, a (biological) male and (biological) female. [...] They can similarly see other kinds of self-referential data even at a young age, well before they program. Isn’t this a horrific example for recursion? Wouldn’t most adults realise that the structure isn’t a tr…

How is a family TREE not a tree

Eventually your (for example) great*N paternal grandfather is going to show up on your mother's side, forming a loop. That's just due to bounds on the human population and how the number of labels for previous generations grows exponentially backwards in time.

Re: How not to teach recursion (2021)

#109
post #52

Earlier quoted context omitted.

i think the problem is that most people try to follow the recursions in their head which is hard. the biggest lesson regarding recursion is that if you're lucky enough to have your problem fit with tco (or have guarantees your problem is small enough), it's actually way simpler to both write and verify. write the base case, write the inductive step, translate to code, done. no hard reasoning about the code required b…

>i think the problem is that most people try to follow the recursions in their head which is hard. It's hard for computers, too as it can easy result in stack overflow if there are too many steps.

that's the obvious issue. another less intuitive one is that modern cpus and compilers are essentially optimized for iterative algorithms. state variables can be kept in registers, simd can be used for data that appears in contiguous arrays, caching is built on locality assumptions and data locality is preserved.

non-flattened recursive algorithms spread their state across linear memory with a full stack frame for every iteration. at the least this defeats gains from caches, at worst it defeats them entirely by filling them with junk.

but, if the problem is small enough it can be worth it for the correctness guarantees and increased simplicity.

trivial iterative algorithms are simpler, sure, but when things get more complicated, recursion can replace very hard to understand, test and debug iterative code with simple recursive definitions where guarantees of correctness and termination come for free.

Re: How not to teach recursion (2021)

#110
post #93

Earlier quoted context omitted.

This might be the root of the problem for the author: >>> In principle, natural numbers are also recursive data: a number is either zero or the successor of another natural number. However, this way of thinking is not natural to students... That's because students didn't learn math. "Back in my day," we learned mathematical induction sometime in middle school, and were re-introduced to it from time to time as a style…

Induction isn't recursion. Induction is why recursion works. Unless you are assuming ZFC, which is much more comlplex then recursion itself, in second-order logic Induction is an axiom that we use to avoid recursion, by setting up the recursive step and then saying "so we don't have to execute it and look further" Constructing the naturals is dynamic programming (building up), not recursion (breaking down). Calculati…

We hadn't gotten to those kinds of concepts in school by the time I learned recursion. The math that confounds programmers is much more basic.
Post reply on HN