Hacker News new | ask | show | jobs
by jfmengels1 1570 days ago
Factorial was taken as an example (in this post) exactly because it is such a common textbook example, which also happens to be bad example in the sense that it is not stack-safe.

As for walking a tree, I agree, but this optimization does not work well with trees shrug, making lists a nicer example in my opinion.

As a teaching method, I feel that factorial is a relatively good example because it is fairly easy to grok in your head or on paper. Recursing through a tree adds a lot of mental overhead, so I feel like it could be a follow-up example to factorial, but it's probably too hard as an initial example. Also, switching to a stack-safe function using recursion is again much easier for factorial than it would be for traversing a tree.

1 comments

Maybe this is a neurodiversity thing but for me going backwards to calculate something like a factorial recursively is hard to think about. It's exactly the same as if someone was trying to teach something about structural integrity in architecture and they said "OK you want to build a staircase. Imagine you're hovering in the air and you start to build stairs down..." I can't concentrate on whatever point they're trying to make with the example because I can't really get over the fact that it's easier to build from the bottom and not hover in the air in the first place.
Maybe try to imagine it from the architects point of view first, instead of from the brick layers? Like in an isometric drawing, for instance?