6 ms·
> Tail recursion is clearer and easier to reason about than loops tail recursion is a complex and subtle expression of control flow that requires substantial b
by preseinger 4y ago
> Tail recursion is clearer and easier to reason about than loops
tail recursion is a complex and subtle expression of control flow that requires substantial background knowledge to be able to even understand, much less reason about based on code on a page
for loops are immediately intuitive to anyone, even without any programming training
no idea how you can come to this conclusion. just ain't so
- kazinator 4y ago> tail recursion is a complex and subtle expression of control flow That is simply nonsense; it's just function application, ideally without having to think about state (or as little state as possible). > for loops are immediately intuitive to anyone, for loops are immediately intuitive to anyone That's just hand-waving without some sort of psychological data. Even if it were true, it would not be relevant because you can't just hand over software maintenance to just "anyone" pulled off the street who finds some language feature intuitive. (In my anecdotal experience, on the contrary, non-programmers have a very poor intuition for the idea of changing variables by assignment and what that means when a backwards jump takes place.) A solution expressed recursively is demonstrably, quantifiably easier to reason about informally or prove correct than loops and state variables. Of course, not to "anyone" with no programming background just pulled off the street, but to people who have the understanding and skills. There is simply less proof material. An entire body of proof techniques that are required with imperative loops are absent. You just use straightforward inductive reasoning instead of pre and post conditions over stateful variables, and loop invariants and whatnot. Anyway the idea that for loops are somehow easier than recursion is definitely not from mainstream CS; just some fringe view fr
- l33t233372 4y ago> the idea that for loops are somehow easier than recursion is definitely not from mainstream CS I have never in my life heard a programmer say they think recursion is easier than loops.
- kazinator 4y agoThat only shows you haven't known any programmers who had a broad exposure to the topics of their supposed craft; it doesn't speak to the actual topic itself. Recursive solutions being easier to verify is quantifiable. This is not some popularity poll. Some people are not well-versed in some techniques. Recursion is not always well supported in programming languages. In standard C if we want to use recursion, we will have to write multiple standalone functions that have their own scopes, whereas if we put together several loops, we can have those all in the same scope, with convenient access to common local variables. That could make a decisive difference. Not all languages have tail calls; what looks like tail recursion can "blow the stack". When I say that it's "easier" I don't mean that anyone of any skill level and background can more easily design and implement a recursive solution for any problem, and in any language. Rather, that when the recursive solution is discovered, it is easier to convince oneself that it is correct: that it's handling all the cases and terminates, with the correct value.
- l33t233372 4y ago> Recursive solutions being easier to verify is quantifiabl Ease of doing something is a quantifiable description?
- preseinger 4y agoverifiability is not an important property of a program, except in very niche circumstances
- preseinger 4y agothe only programmers who are concerned with proofs are located in universities and are writing theses, which are statistically 0% of programmers overall programs are recipes, not proofs. "for 10 times, do this" is in almost all cases trivially easier to understand and maintain than a recursive alternative. this isn't controversial in any way