News for hackers. Stuff that matters.
Re:Misconceptions about loops in C by dist-epoch on Thursday June 27, 2024 @03:03PM (#40813864)
That's like saying "it's unfortunate kids are first taught elementary arithmetic before integrals". C-style loops are natural and easy to understand, tail recursion is not.
Re:Misconceptions about loops in C by chongli on Thursday June 27, 2024 @03:58PM (#40814439)
Recursion is more natural and deeply-engrained than people realize. Natural language has a recursive grammar:

My dog => My friend's dog => My father's friend's dog => My father's sister's cousin's nephew's teacher's friend's plumber's dog's chew toy.

We can construct sentences like this with a completely arbitrary level of recursive nesting. It's so natural that people do it without thinking about it. Recursion only becomes unnatural when we prime people on it and get them thinking it's scary and complicated.

Re:Misconceptions about loops in C by atorodius on Thursday June 27, 2024 @04:12PM (#40814579)
I don’t think this is true at all. I remember vividly when I learned about loops („oh it is just the statement multiple times“) vs learning recursion (spending 3d on an assignment from uni and scratching my head and not getting how the heck this makes sense)
Re:Misconceptions about loops in C by slashdave on Thursday June 27, 2024 @04:24PM (#40814694)
So natural, yet the specific example you provide is broken
Re:Misconceptions about loops in C by AlotOfReading on Thursday June 27, 2024 @04:26PM (#40814714)
I'm surprised you consider that construction "simple". Recursive relationships like that are used for brain teasers and even a famously absurd joke in Spaceballs.

    Darth Helmet: I am your father's brother's nephew's cousin's former roommate.

    Lonestar: What does that make us? 

    Darth Helmet: Absolutely nothing.
In fact, standard English tends to avoid recursive relationships with specialized kinship terms based on ordinals like first cousins, first cousins once removed, great-, and so on.
Re:Misconceptions about loops in C by chongli on Thursday June 27, 2024 @04:29PM (#40814746)
I'm surprised you consider that construction "simple".

It's VASTLY simpler than the alternative. See how difficult it is to express the same relationship without using the recursive grammar.

Anyway, give anyone a family tree diagram containing all of these relationships and they can follow the chain from that sentence to the destination. This is the essence of why we use recursion in computer science in the first place: it's the best tool for navigating trees.

Re:Misconceptions about loops in C by com2kid on Thursday June 27, 2024 @06:16PM (#40815784)
> Anyway, give anyone a family tree diagram containing all of these relationships and they can follow the chain from that sentence to the destination. This is the essence of why we use recursion in computer science in the first place: it's the best tool for navigating trees.

This is because English sucks at tree relationships. Other languages are much better at this.

For example, Mandarine Chinese has unique words for each side of the family tree (e.g. unique word for Grandma on mother's side vs Grandma on Father's Side), and also a rather logical system to describe how you navigate the tree.

Chinese isn't even unique in this, it is just that English is really really bad.

Re:Misconceptions about loops in C by dfawcus on Friday June 28, 2024 @11:01AM (#40821306)
Is having a unique word that much of an improvement over "paternal grandmother" vs "maternal grandmother"?

Or is it that you're referring to other relations than those two having unique words? If so then that would seem to introduce its own problems in ballooning the vocabulary.

Maybe English is just happier with the ambiguity?

Re:Misconceptions about loops in C by com2kid on Friday June 28, 2024 @11:48AM (#40821843)
One word helps make it easier.

I got very tired, very quickly, even as a kid, of saying "Grandma on my Dad's side".

I had two Uncles with the same name, again, "Uncle <foo> on my Dad's side" is a PITA after awhile.

Re:Misconceptions about loops in C by svachalek on Thursday June 27, 2024 @03:08PM (#40813909)
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.
Re:Misconceptions about loops in C by mbivert on Thursday June 27, 2024 @03:20PM (#40814044)
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.
Re:Misconceptions about loops in C by Gibbon1 on Thursday June 27, 2024 @04:25PM (#40814707)
People that see programming as math get really excited about recursion.

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

Re:Misconceptions about loops in C by timeinput on Thursday June 27, 2024 @06:00PM (#40815631)
There's this summation notation that these people who see programming as math and get excited about recursion forget about too.
Re:Misconceptions about loops in C by mbivert on Thursday June 27, 2024 @06:19PM (#40815810)
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.

Re:Misconceptions about loops in C by burnished on Thursday June 27, 2024 @11:28PM (#40817422)
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.
Re:Misconceptions about loops in C by zelphirkalt on Friday June 28, 2024 @12:03PM (#40822007)
Recursion is a form of iteration.
Re:Misconceptions about loops in C by mbivert on Friday June 28, 2024 @04:54PM (#40825191)
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.

Re:Misconceptions about loops in C by eigenket on Thursday June 27, 2024 @03:33PM (#40814179)
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.

Re:Misconceptions about loops in C by KineticLensman on Thursday June 27, 2024 @04:10PM (#40814557)
> 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

Re:Misconceptions about loops in C by warkdarrior on Thursday June 27, 2024 @05:08PM (#40815128)
You guys should write these crochet patterns in continuation passing style!
Re:Misconceptions about loops in C by playingalong on Thursday June 27, 2024 @03:50PM (#40814344)
Rinse and repeat.
Re:Misconceptions about loops in C by tedunangst on Thursday June 27, 2024 @03:17PM (#40813996)
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.
Re:Misconceptions about loops in C by klyrs on Thursday June 27, 2024 @03:41PM (#40814259)
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.

Re:Misconceptions about loops in C by adwn on Thursday June 27, 2024 @04:17PM (#40814621)
> 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.

Re:Misconceptions about loops in C by klyrs on Thursday June 27, 2024 @06:07PM (#40815699)
The first paragraph was more of a parody of your just-so argument than point per se; but the second paragraph is literally recursion.
Re:Misconceptions about loops in C by nequo on Thursday June 27, 2024 @04:54PM (#40814984)
How is it not recursion?

  def spawn():
    take_shop_class()
    return spawn()
Re:Misconceptions about loops in C by svachalek on Thursday June 27, 2024 @07:58PM (#40816497)
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.
Re:Misconceptions about loops in C by salawat on Sunday June 30, 2024 @06:04PM (#40840759)
//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.

Re:Misconceptions about loops in C by paulddraper on Thursday June 27, 2024 @03:19PM (#40814035)
Recursion is more beautiful.

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

Re:Misconceptions about loops in C by pineapple_sauce on Thursday June 27, 2024 @03:27PM (#40814110)
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).

Re:Misconceptions about loops in C by mbivert on Thursday June 27, 2024 @06:08PM (#40815709)
> 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.

Re:Misconceptions about loops in C by o11c on Thursday June 27, 2024 @07:05PM (#40816132)
Often I get the impression that beauty is defined as "how far is this from what the computer can actually do efficiently?"
Re:Misconceptions about loops in C by paulddraper on Thursday June 27, 2024 @08:09PM (#40816535)
> How do you measure beauty?

Using the beholder's eye, of course.

Re:Misconceptions about loops in C by vsuperpower2020 on Thursday June 27, 2024 @08:26PM (#40816639)
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.
Re:Misconceptions about loops in C by burnished on Thursday June 27, 2024 @11:31PM (#40817438)
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?

Re:Misconceptions about loops in C by paulddraper on Thursday June 27, 2024 @09:04PM (#40816848)
https://en.wikipedia.org/wiki/Mathematical_beauty

Simplicity is actually a hallmark of this sense of beauty.

Re:Misconceptions about loops in C by burnished on Thursday June 27, 2024 @11:33PM (#40817451)
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.
Re:Misconceptions about loops in C by xigoi on Friday June 28, 2024 @03:56AM (#40818622)
Too bad C doesn’t have a “do this 3 times” loop.
Re:Misconceptions about loops in C by photochemsyn on Thursday June 27, 2024 @03:58PM (#40814429)
Or like saying they should start their study of addition and multiplication from the perspective of group theory - since the integers (Z) under addition forms a group because it satisfies closure, associativity, identity, and inverses, while Z under multiplication does not form a group because, although it satisfies closure, associativity, and identity, it fails to provide inverses for all elements.
Re:Misconceptions about loops in C by com2kid on Thursday June 27, 2024 @06:18PM (#40815799)
I'd like to remind everyone that the computer doesn't care. The computer knows JMP (e.g. GOTO).

Everything else is just abstraction's we're built up to make our lives easier.

If we always jump to the top of the function, or if we jump someplace else, the machine cares not. Just don't blow the stack.