Live data from Hacker News

Stop Telling Students Recursion is Hard

jinfiesto.posterous.com

81–90 of 111 posts

Re: Stop Telling Students Recursion is Hard

#81
post #11

Am I along in never being "taught" recursion? I mean, throughout my curriculum, I've used recursion many times and dealt with everything from fibonacci to quicksort, but never once have I sat through even part of a lecture on "Recursion". Among my peers, I don't think anyone really has an issue with recursion or how it works. Harder to debug possibly, but everyone "gets" it, I feel.

Shouldn't it be easier to debug, because you have stack traces?

Re: Stop Telling Students Recursion is Hard

#82
post #12

Earlier quoted context omitted.

Agreed - the problem is not recursion, but that it becomes useful for actual complex problems. I understand recursion pretty well at a conceptual level (my background is math, mostly self-taught for CS), but I have a hard time using it for programming. Sure, fibonacci or quicksort are simple, but tree recursion or application to string processing (e.g. for edit distance compuation) is quite harder.

I am the only one who gets upset when fibonacci sequence calculation is used as an example of recursion? Are there other possible examples which are simpler than quicksort, but actually make sense if done with recursion instead of simple loop?

No, I think it is a terrible example at so many levels it is not even funny. The only interesting thing about that example is how almost any other solution is much better.

I would consider quicksort to be a quite simple example if you use a high level language. But search in binary search tree and similar tree/graph traversal are maybe better examples, in the sense that most people would find recursion to be more natural than any iterative solution.

Re: Stop Telling Students Recursion is Hard

#83
post #69
post #68

Earlier quoted context omitted.

That's certainly a way of representing it. But surely it's equivalent in practice (as long as the next step can't be a nested flight of stairs) to the iterative approach: while there are stairs in front of me climb one stair I guess it's just not as clear to me as it is to you that the "up" utterance is associated with the "climb the stairs" procedure rather than the "climb one stair" procedure.

I agree that it's equivalent in practice; both programs could compile to the same machine code. Where it's different is the human thinking represented by the two programs. The word "while" implies thinking about a range of time from beginning to end. In contrast, the non-"while" version lets you stay in the moment. It's a conceptually simpler program, up until the point we use that fancy mathematical r-word to descri…

You're still talking about state, yes? In either case you see a flight of stairs, you start executing "climb the stairs" and that continues until you are no longer climbing stairs.

It seems to me that "climb one stair as long as there are stairs" is a conceptually simpler definition than "climb one stair and then climb the stairs if there are still stairs."

I think my point still is I don't see how you're differentiating between these procedures in your daughter's mind— why you're assuming that "up" means "I need to climb all of these stairs" rather than "I need to climb this one stair in front of me."

(If indeed it makes sense to model human behavior either way at all; it probably makes the most sense to think of it more as an event loop...)

Re: Stop Telling Students Recursion is Hard

#84
post #63
post #30

Recursion is a natural idea. When humans perform repetitive tasks, we don't assign state variables, and we generally don't keep counters. We just keep doing the same thing over and over until we arrive at some kind of terminating condition. That's a while loop. To eat a bowl of Cheerios, keep spooning Cheerios into your mouth while there are more Cheerios in the bowl. Telling students that recursion is hard isn't a g…

I've been hearing experienced programmers opine for a long time that while is the more natural representation of repetitive tasks. Twelve years ago my oldest was learning to walk upstairs. I watched her say "up", go up one stair, then say "up" again, and go up the next stair. This concrete example of recursively calling the "up" function did more to convince me that recursion is natural than any explanation with abst…

Both iteration and recursion result in repeated operations. Either way you define the operation of going up a flight of stairs, it's going to involve going "up" a step over and over again, so there's no way to know which way your kid was thinking just from the fact that she said "up" over and over again. In the "while" scenario, the higher-level function "up the staircase" is done once and the lower-level function "up the next step" is performed many times to accomplish it. In the recursive scenario, "up the stairs" is defined in terms of "up the stairs." Climbing the last step is "up the stairs" and it is also part of the "up the stairs" operation of climbing the last two steps, which is part of the "up the stairs" operation of climbing the last three steps.

So if your kid was thinking recursively, the last "up" operation would be part of the first "up" operation and also part of every intervening "up" operation. If you asked your kid on the last step of a twelve-step staircase how many "up" she was doing, she presumably wouldn't answer "twelve;" she would answer "one." Would that be because she performed the obvious optimization of a tail-recursive function, or because she wasn't actually thinking recursively?

Re: Stop Telling Students Recursion is Hard

#85
post #27

When learning recursion, there aren't much practice problems people do in imperative languages. Most of the imperative languages don't have tail call optimization and the prevalent culture is to use iteration. That said, there are some problems for which the iterative solution won't be straight forward viz. quicksort, mergesort, binary tree traversal etc etc. But the issue is when a student not very familiar with rec…

Same experience here, though I learned recursion in Pascal. Maybe it's just me, it just feels awkward when you do recursion in imperative languages. For the past two years, I've been playing/working with functional languages, firstly Erlang or more recently F#, as well as functional programming in C#, and start to appreciate the power of FP. Recursion is also used more often as it just feels natural there when you do recursion in FP.

Re: Stop Telling Students Recursion is Hard

#86
post #26

When I was writing Ansi Common Lisp I spent a while thinking about why students find recursion a difficult concept. I decided it was because they think of a function as a machine for doing something. E.g. the length function as a machine that finds the lengths of lists. If you think of a function instead as a definition-- e.g. as the definition of length-- then suddenly it's not difficult anymore. At least, that was…

Robert Harper from CMU, eloquently expresses the same notion: "One of the classic pitfalls of teaching recursion is to try to explain it by referring to some mysterious entity called a “stack” that is absolutely invisible in the program you write. There’s a long song and dance about “frames” and “procedure calls” and “pushing” and “popping” things on this “stack” of which the professor speaks, and no one understands a damn thing. It all sounds very complicated, and impressive in a way, but if there’s one thing people learn, it’s to stay the hell away from it! And in doing so, students are robbed of one of the most powerful and useful programming tools available to them, the key to writing clean, reliable code". more: http://existentialtype.wordpress.com/2011/03/21/the-dog-that...

Re: Stop Telling Students Recursion is Hard

#87
post #69
post #68

Earlier quoted context omitted.

That's certainly a way of representing it. But surely it's equivalent in practice (as long as the next step can't be a nested flight of stairs) to the iterative approach: while there are stairs in front of me climb one stair I guess it's just not as clear to me as it is to you that the "up" utterance is associated with the "climb the stairs" procedure rather than the "climb one stair" procedure.

I agree that it's equivalent in practice; both programs could compile to the same machine code. Where it's different is the human thinking represented by the two programs. The word "while" implies thinking about a range of time from beginning to end. In contrast, the non-"while" version lets you stay in the moment. It's a conceptually simpler program, up until the point we use that fancy mathematical r-word to descri…

The word "while" implies thinking about a range of time from beginning to end. In contrast, the non-"while" version lets you stay in the moment.

I disagree. The word "while" does connote time, but it's just the word chosen by early programmers to describe a process of performing an action immediately depending on current conditions, with no knowledge of history. Recursion amounts to the same thing -- after you make the mental leap of a tail-recursive optimization. When you climb the next step, you're climbing it because it's part of the definition of climbing the rest of the steps, which was part of the definition of climbing the rest of the steps when you were on the previous step. As I pointed out in another comment, when you're climbing the last step of a twelve-step staircase, you're actually performing twelve "climb up the steps" operations that are nested like Russian dolls. Thinking recursively is only simple when you learn to mentally simplify all of that, basically to think in terms of the "same machine code" you mention in your comment. I don't think we arrive at that "machine code" starting from a recursive definition of climbing the stairs. It seems much more likely that we figure out the implementation (the while loop) long before we figure out (if we ever do) that we can derive that implementation from a recursive definition of the task.

Re: Stop Telling Students Recursion is Hard

#88
post #72
post #32

Earlier quoted context omitted.

Why, though, do they think of a function as a machine for doing something? Because we tell them so. Before starting any standard CS curriculum (ignoring, for a moment, self-taught hackers), the only place a student should have heard about "functions" is in math class---where they are definitions. They get to intro Java and we tell them "no, no, that's not a function, this is a function, look, it's got variables and f…

Your "methods" have also been called "procedures".

sure. pick one and make people use it

Re: Stop Telling Students Recursion is Hard

#89
post #27

When learning recursion, there aren't much practice problems people do in imperative languages. Most of the imperative languages don't have tail call optimization and the prevalent culture is to use iteration. That said, there are some problems for which the iterative solution won't be straight forward viz. quicksort, mergesort, binary tree traversal etc etc. But the issue is when a student not very familiar with rec…

/second TLS. Most of the book is on recursion, and uses a Socratic teaching method to both explain and reinforce it brilliantly.

Re: Stop Telling Students Recursion is Hard

#90
post #56
post #35

Earlier quoted context omitted.

Any "divide and conquer" algorithm is likely to be recursive. Thus, for instance, the FFT, the Karatsuba multiplication algorithm for large integers, Strassen's matrix multiplication algorithm for matrices and the like are all recursive. Moving on, dynamic programming is a very important technique. About half the time it is easier for me to figure out a dp solution by writing a recursive solution with memoization tha…

You can talk about "divide and conquer" but the most common problem where recursion really helps is looking at tree data structures. As then to consider looking for the total of some value stored in an XML tree. In an iterative language it's a for loop and then calling the function on each of the children. In a Lisp it's calling it's self on the child and next node. PS: Showing someone they can transform a recursion…

Yes, recursion is a natural way to walk through a recursive data structure, like a tree. However if you only know how to reach for recursion, you're completely hosed the second you need a breadth-first search instead. (I've watched candidates completely collapse when faced with that problem.)
Post reply on HN