Slashdot Mirror


AP Test's Recursion Examples: An Exercise In Awkwardness

theodp writes "Yet another example of how AP exams are loaded with poor coding practices," quipped Alfred Thompson, referring to a recursive code example that prints the numbers 0 to 6, which was posted to the (closed) AP Computer Science Facebook group. "We are often forced to use code examples that are not ideal coding practice," Thompson notes. "We do that to make things clear and to demonstrate specific concepts in a sort of isolation that we might not normally use. We seem to do that a lot with recursion because the examples that require recursion tend to be fairly complex." So, while asking students to use recursion instead of a loop to print '0123456' serves the purpose of teaching recursion, Thompson opines that it's also a poor example of code practice. "Someone raised on functional programming where recursion is a pretty standard way of doing looping might disagree of course," he adds. "There is a saying that when all you have is a hammer all your problems look like nails. This seems, in a way, to be the case with recursion and loops. If your first tool of choice (or what you have learned first) for iteration is loops you tend not to think of recursion as a solution. Similarly if you start with recursion (as is common with functional programming) you are a lot more likely to look at recursion as a tool for iteration." So, do you tend to embrace or eschew recursion in your programming?

7 of 252 comments (clear)

  1. What do you expect? by halivar · · Score: 4, Insightful

    A legitimate use of recursion would probably be something a lot more complicated, and a lot harder to grasp, than a contrived use that teaches the concept in isolation. It's like teaching a kid long division using 6000 / 10, and disparaging the example saying, "yeah, but you would never use long division for that." Well, no shit, Sherlock. You're teaching mechanics. Application is for another lesson. Maybe even another class.

    1. Re:What do you expect? by Daniel_Staal · · Score: 4, Insightful

      I'd argue that the solution to a problem is a lot easier to understand if you're given a context where the solution is needed FIRST. Starting with a degenerate problem that reduces to a trivial application serves to obscure the 'point' of the solution method.

      This isn't the teaching materials. This is a test question. Yes, the teacher should teach the concept with a better example and explain it fully - but the question is enough to show if the student understands the concept and can apply it correctly. It's also quick to explain and short to answer, both good things for a test question.

      This isn't the starting point - this is an ending point. (The end of the class.) The question is enough for that.

      --
      'Sensible' is a curse word.
    2. Re:What do you expect? by ShanghaiBill · · Score: 5, Insightful

      This isn't the teaching materials. This is a test question.

      Exactly. This question is not asking for an implementation. It is just testing whether recursion is understood well enough to predict the result. This is a good question. Someone with a basic understanding of syntax and recursion would be able to answer the question correctly. Someone without that understanding would not.

      Complaining that recursion was used instead of iteration is just silly, since that is not what is being tested. A proper implementation would use neither. If I was tasked with writing a program to list the digits from zero to six, I would write this:

      #!/bin/sh
      echo "0123456"

    3. Re:What do you expect? by Anonymous Coward · · Score: 2, Insightful

      let fib n = fib_aux n 0 1 where fib_aux n a b | n >= 1 = fib_aux (n-1) b (a+b) | otherwise = b

      There you go, linear time, constant space.

      And if you'd actually need Fibonaccis, you'd either use a lookup table for limited precision (~50 entries for 32 bit, ~100 for 64) or something like unfolded version of matrix exponentiation for arbitrary precision.

  2. directory recursion simple example of WHY and how by raymorris · · Score: 4, Insightful

    Recursing a directory/folder structure would be a much better example of not only HOW to use recursion, but when and why it actually makes sense.

    TFS asks "do you use recursion?". When it makes sense, such a directory or category tree. That is, when the problem is inherently recursive. It's also good to know that all recursive algorithms can be trivially converted to iterative versions with an explicit stack or queue. This is useful for something that could recourse deeply, like following links to spider the web.

  3. Inherent Conflict in writing vs. reading by Tablizer · · Score: 3, Insightful

    In the real world it's best to write for a "lowish" common denominator because future staffing is unknown, but best to know HOW to figure out "clever" code if you encounter it. Thus, the best habits for writing code tend to conflict with learning to read any code you may encounter.

    The solution is to point out early that writing code is similar to writing documentation: clarity to other readers matters above parsimony etc. Put that on the test.

  4. So strange! by drolli · · Score: 4, Insightful

    I mean, this guy really seems not to get that the excercise was not meant to demonstrate how to print 123456, but that printing 123456, as opposed to e.g. 654321 is a tool to demonstrate/show the order of the execution of command in a self-recursive function.

    I mean, i have seen bad coding practices in courses (nervous handling of null pointers, and strong binding OOP comes to my mind), but this question has nothing to do with coding, but is purely the shortes way to test is the student understands what happens if the print comes after the call of the self recursive function.