4 ms·
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 recur
by dist-epoch 2y ago
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.
- svachalek 2y agoC 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.
- tedunangst 2y agoThis 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.
- klyrs 2y agoExactly. 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.
- adwn 2y ago> 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.
- svachalek 2y agoC 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.
- salawat 2y ago//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.
- paulddraper 2y agoRecursion is more beautiful. But take a newbie, and "do this 3 times" in a loop is far more clear than the recursive equivalent.
- pineapple_sauce 2y agoHow 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).
- mbivert 2y ago> 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.
- o11c 2y agoOften I get the impression that beauty is defined as "how far is this from what the computer can actually do efficiently?"
- paulddraper 2y ago> How do you measure beauty? Using the beholder's eye, of course.
- vsuperpower2020 2y ago
- mbivert 2y agoIteration 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.
- Gibbon1 2y agoPeople that see programming as math get really excited about recursion. Everyone else doesn't care, shouldn't care.
- timeinput 2y agoThere's this summation notation that these people who see programming as math and get excited about recursion forget about too.
- mbivert 2y agoI 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.
- burnished 2y agoHey, 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.
- zelphirkalt 2y agoRecursion is a form of iteration.
- mbivert 2y agoWorse: 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.
- eigenket 2y agoI 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.
- playingalong 2y agoRinse and repeat.
- KineticLensman 2y ago> 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
- warkdarrior 2y agoYou guys should write these crochet patterns in continuation passing style!
- photochemsyn 2y agoOr 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.
- chongli 2y agoRecursion 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.
- atorodius 2y agoI 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)
- slashdave 2y agoSo natural, yet the specific example you provide is broken
- AlotOfReading 2y agoI'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.
- chongli 2y agoI'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.
- com2kid 2y agoI'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.