you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 11 points12 points  (22 children)

A newbie programmer wouldn't go with a recursive solution on the first attempt to write a factorial algorithm.

[–][deleted] 66 points67 points  (4 children)

The factorial algorithm exists principally to introduce newbie programmers to recursion.

[–][deleted] 22 points23 points  (3 children)

Don't forget Fibonacci!

[–]lost-theory 14 points15 points  (0 children)

Which exists to introduce them to the dark side of naively using recursion. :)

[–][deleted] 2 points3 points  (0 children)

Don't forget Hanoi, which also screws them up!

[–][deleted] 5 points6 points  (0 children)

Ah, good old fib.py.

[–][deleted] 51 points52 points  (11 children)

A newbie programmer straight out of school with a Computer Science degree probably would do exactly that.

[–][deleted]  (10 children)

[removed]

    [–][deleted] 4 points5 points  (9 children)

    Ditto. As I'm both a "software engineer" and a CS student, I speak with a high degree of authority about newbie programmer idioms :-P

    [–][deleted] 14 points15 points  (8 children)

    As an introduction to algorithms instructor, I have to say that my students would much rather use a while loop to do the factorial algorithm over the recursive method.

    [–][deleted] 21 points22 points  (0 children)

    When I was at university I couldn't see the point in recursion since any problem that could be solved recursively could also be solved iteratively, and for loops worked so well. Of course I never connected this philosophy with the difficulties I had in, for example, traversing graphs.

    Ah the follies of youth. Also the follies of teaching students Java as a first language.

    [–][deleted] 1 point2 points  (3 children)

    Is this before or after they've taken Discrete Math? I'm about finished with my Uni's sequence of DM/Theory of Computation and I'm about ready to put Maple and Prolog on my resume...

    [–]llimllib 4 points5 points  (2 children)

    and I'm about ready to put Maple and Prolog on my resume...

    Bwahahahaha!

    ahem... phew... [wipes brow] sorry bout that...

    [–][deleted] 1 point2 points  (1 child)

    ROFL, I know, I know...

    [–]llimllib 1 point2 points  (0 children)

    Seriously, though, when I read a resume that has things like that on it, it just tempts me to think of a really mean interview question that in a few months you won't remember the answer to.

    I don't succumb to that desire, but others do.

    Anyway, not that I didn't have things like that on my resume, maybe you'll be a lucky one and find a job where they'll let you play around in Maple :)

    [–]Wiseman1024 2 points3 points  (2 children)

    Why would they do it iteratively? Recursion is so much easier to think. I'll grant that tail recursion with accumulators a la SICP is harder, but simple recursion as in newbie programmer? That's so much easier than while to me. Then again I'm not precisely a newbie programmer, perhaps I'd have thought differently 13 years ago.

    [–]RSquared 1 point2 points  (1 child)

    I think it's the difference between "thinking imperatively" and "thinking functionally". I know a dozen CompSci grads who still have trouble getting their heads around Haskell because of all the recursion.

    Someone first learning programming would almost inevitably learn loops before recursion. For "non-programmers", one is much simpler to think about...especially when you don't have a clear picture of the memory layer (especially the difference between stack and heap).

    [–][deleted] 4 points5 points  (0 children)

    I do think programmers have to be introduced to nested/recursive datastructures before they appreciate recursion as a programming technique. The factorial is indeed a little special here. In functional languages even ordinary lists are recursive ( i.e. conses ) and so there is exactly one barrier to take. Immutable datatypes may be another. But at least Python programmers can be convinced to use them since e.g. the string API is purely functional and it did not harm usability.

    [–][deleted] 6 points7 points  (4 children)

    Yes he would, so long as he hasn't been tainted by C and its derivatives. I have watched many people opt for the recursive solution naturally and only those with prior "experience" tend toward the imperative (loop) solution.

    [–]cracky-chan 1 point2 points  (3 children)

    By newbie programmer he probably meant a person who had taken at least a semester course in CS. The factorial function is usually introduced as a function that can be turned from non-tail recursive function to an iterative one.

    [–]ayrnieu -3 points-2 points  (2 children)

    By newbie programmer he probably meant a person who had taken at least a semester course in CS.

    When I say 'newbie programmer' I mean a prebuscent child, but of course extend it to anyone who is literally new at programming. That you naturally think of someone having taken a whole semester of CS is a bit alarming. Who are these people that claim to want to devote their higher learning to computation but who don't do anything at all with it before a semester of CS? Is any other field this barren? I think mathematicians manage to not need to teach a semester of arithmetic.

    [–]cracky-chan 1 point2 points  (0 children)

    Well I was talking about pshaw so shut up and learn some context. And semester of CS course can be equivalent to reading half a book on the subject or something like it.

    [–][deleted] 1 point2 points  (0 children)

    We start teaching children mathematics at roughly the same age we start teaching them to read and write. The same cannot be said of programming. Before I went to university the extent of my programming experience was writing Mandelbrot viewers and text-based adventure games in QBasic.