Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

C loops are natural and easy to understand for programmers who were trained on loops. I doubt that tail recursion is any harder to get for students who don't have that background. Would be an interesting experiment though.


Iteration is more natural for most people than recursion. There are a few aliens for whom recursion is more natural, but iteration matches more closely daily life.


People that see programming as math get really excited about recursion.

Everyone else doesn't care, shouldn't care.


Hey, other kinds of needs can do and should care. Recursion is a very interesting perspective to apply to a problem and gratifying in its own right.


There's this summation notation that these people who see programming as math and get excited about recursion forget about too.


I think recursion is just another tool in the toolbox: sometimes, some solutions are easier to think of recursively, and so it makes sense to use it, at least for the first prototype.

But yeah, using it systematically (in a non-functional language) is unwise.


Recursion is a form of iteration.


Worse: both are theoretically equivalent.

But this doesn't change the fact that humans seem to intuitively perceive iteration as more natural.

Like, "space" and "time" can't be understood as two distinct notions, per relativity. But we still perceive them as such on a daily basis.


I think its fairly clear that loops are more natural and obvious for most people for most tasks.

There are examples of other communities which have invented something pretty much the same as loop syntax, for example you get things which are basically loops in knitting patterns or (less reguarly) in recipes. I have never seen an example of recursion outside computer science/programming.


> for example you get things which are basically loops in knitting patterns

Also in crochet. A pattern will have instructions like

* (2tr, 3ch, 2tr) all in next 2ch sp; repeat from * 6 times more

where statements like 2tr mean 2 treble stitches and the bit between the * symbols is repeated 6 times


You guys should write these crochet patterns in continuation passing style!


Rinse and repeat.


This is what happens when we eliminate shop class. Sand this table leg. Good. Now do that three more times. Kids used to know loops before they ever touched a computer.


C doesn't have a "do this 3 times" loop though. It has an "initialize counter to 0 first, increment counter after each operation, and compare the counter to 3 (or is it 4?)" loop. Kids definitely don't get this without being taught.


//Precon: Need 4 legs. Have none.

Int counter_of_sanded_legs = 0;

do{

Sand_a_leg; Counter_of_sanded_legs++;

} While (counter_of_sanded_legs < 4);

Need four legs, have none. Doing while I don't have enough. How many legs do I have? 1. Doing. How many legs do I have? 2. Doing. How many legs do I have? 3. Doing. How many legs do I have? 4. Done.

Alternatively:

for (int i=0;i<4;i++;) {

//how we start; whether we repeat; how we update our counter.

Sand_leg();

}

You only need to sand a leg when you don't have enough legs.

These aren't that far off of actual things you'd do moving about. In fact, not realizing that you're distilling something physical into the symbolic is a leading cause of confusion in my experience.

A "do x times" dedicated looping construct would be syntactic sugar that detracts from understanding what the underlying semantics are; which if you're teaching how to program is a bad idea. The idea is to have your understanding precede the machine. Not lag behind.

You can start screwing around with abstractions after you get the basics. As once you have the basics, you can compose them into any form you want.

The rest is just increasing levels of crippling mental illness.


Exactly. Go make a table with 4 legs and also every leg has 4 legs. Now you get recursion and a very steady table.

You were a kid in shop class. Now you want your kids to take shop class. Because, recursion.


> Go make a table with 4 legs and also every leg has 4 legs.

If you have to make up nonsensical examples to prove your point, then you probably don't have a point.

> You were a kid in shop class. Now you want your kids to take shop class. Because, recursion.

That's not recursion.


The first paragraph was more of a parody of your just-so argument than point per se; but the second paragraph is literally recursion.


How is it not recursion?

  def spawn():
    take_shop_class()
    return spawn()


Recursion is more beautiful.

But take a newbie, and "do this 3 times" in a loop is far more clear than the recursive equivalent.


How do you measure beauty? You can't: "beauty" is subjective. And even if you try e.g. count the times you use recursion vs. iteration: that metric is subjective and not grounded in reality.

Sometimes recursion does allow you to reason about code more easily or come to a working solution faster, sometimes it does not.

Measure the concrete: CPU time and memory consumed. Iteration will likely trump recursive methods w.r.t both these metrics. If it doesn't, you can likely transform your iterative algorithm to one that utilizes SIMD (not always).


> How do you measure beauty? You can't: "beauty" is subjective

Let me try: in classical dance, martial arts, or even skateboarding, advanced skills manifest as effortlessness: the movements comes naturally, they're not forced, things just flow.

If you compare a typical functional (recursive + pattern matching, but the point would stand even with a fold) factorial with an imperative one (for loop), the functional approach is more effortless, you have to be less explicit about what's going on. It's more eloquent.

However as you seem to imply, when we're programming, the focus should be on delivering something that works as expected; this particular kind of aesthetic is secondary at best.


> How do you measure beauty?

Using the beholder's eye, of course.


Often I get the impression that beauty is defined as "how far is this from what the computer can actually do efficiently?"


This is a painfully stemlord response. 'Aesthetic qualities cannot be objectively measured therefor they do not exist'. Christ. Its fine to pick a metric to evaluate one thing against another, its another to deny that there are other ways to value things.


I never understood anyone who used such strong language like "beauty" when talking about something mundane like recursion. I suspect it's just in-group language like how a cult understands the meaning of their own words, while to outsiders it would appear nonsensical. I have seen other groups appropriate the word "beauty" too, like people who chronically overeat.


https://en.wikipedia.org/wiki/Mathematical_beauty

Simplicity is actually a hallmark of this sense of beauty.


Nope, it just has its own beauty. The material blew me away in school.

What culty group even exists around recursion anyway? Can you get me an invitation?


Too bad C doesn’t have a “do this 3 times” loop.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: