Re: Recursive functions



army1987@xxxxxxxx writes:
Keith Thompson ha scritto:
In real-world C code, I agree. <OT>In Lisp-like languages, it might
be perfectly appropriate.</OT>

But homework assignments are not real-world code; consider how easily
we can tell the difference when people post here asking for help. The
canonical first program is "Hello, world". That's not something for
which there's any real-world requirement.
But it *is* useful to know how to print a message to stdin from within
a real-world C program (it is done all time)...

I assume you mean stdout, not stdin. But that's not the main point of
the "hello, world" program. It's certainly one thing a student will
learn from it, but the main point is to learn how to write, compile,
and execute a program.

Similarly, problems that actually require recursion tend to be more
complex than might be appropriate for a beginner, but we *can* teach
the elements of recursion using artificially simple example, like
computing the length of a string. It might be appropriate to mention
in passing that recursion really isn't the best solution (in fact, a
call to the strlen() function is) -- and for all we know, the OP's
instructor might have mentioned that.
...whereas it is *totally* *useless* to know how to recursively
implement an algorithm which is 1) much, much better implemented
iteratively, and 2) already implemented by the standard library.

The point is not to learn how to compute the length of a string. The
point is to learn about recursion. Introducing the concept of
recursion via a simple task allows the student to learn about the
concept without a lot of extraneous details getting in the way.

How did you first learn about recursion?

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Recursive functions
    ... But homework assignments are not real-world code; ... canonical first program is "Hello, ... the elements of recursion using artificially simple example, ...
    (comp.lang.c)
  • Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
    ... that deliberately went one beyond the end of the string, ... Within its framework, which sucks, it is a correct program. ... To do this you changed a value parameter to a reference parameter and ... this makes the recursion go wrong. ...
    (comp.programming)
  • Re: Recursive functions
    ... recursion is certainly a natural way of defining the ... it IS totally inappropriate to compute ... not mean "inefficient in a typical C implementation". ... If I had an actual requirement to print the string ...
    (comp.lang.c)
  • Re: Three Kinds of Logical Trees
    ... > I think this is just a representation issue. ... > I do have a number, but I am only ttreating as a string. ... I was attempting to isolate the question of tree structure; ... it's not much of an issue; no one uses recursion much. ...
    (comp.databases.theory)
  • Is this recursion?
    ... This is the ole reverse a string classroom assignment. ... Recursion is soemthing I never quite understood till now but as I ... public static void reversal{ ...
    (comp.lang.java.help)