News for hackers. Stuff that matters.
Re:Dijkstra was right – recursion should not be difficult by JepZ on Monday August 07, 2017 @03:05PM (#14950274)
Some years ago I had problems with recursions too, because I always had some edge cases which made my functions go wild (endless loops). But one day someone told me, that most recursive programs can be written into the skeleton below:

  function a(){
    if(/* Termination Condition */){
      return /* Special Element */
    } else {
      /* Recursion: e.g. a + a */
      return a() + a()
    }
  }
Since that day I love recursions.
Re:Dijkstra was right – recursion should not be difficult by LyndsySimon on Monday August 07, 2017 @04:20PM (#14950917)
If you're using a language with pattern matching (cough Haskell cough), you don't even need the conditional...
Re:Dijkstra was right – recursion should not be difficult by eru on Monday August 07, 2017 @07:05PM (#14952029)
Or rather, the language recognises the conditional and makes sure you handle all the cases.
Re:Dijkstra was right – recursion should not be difficult by remotebug on Thursday August 10, 2017 @08:13AM (#14978836)
Thank you so much for this!
Re:Dijkstra was right – recursion should not be difficult by AstralStorm on Monday August 07, 2017 @03:10PM (#14950309)
This is tree recursion that is not well optimized by modern compilers. AS opposed to tail recursion.
Re:Dijkstra was right – recursion should not be difficult by aidenn0 on Monday August 07, 2017 @06:24PM (#14951767)
It is very rare that the tail-recursive form of a function is more readable than the non-tail recursive form. I find tail-recursion intellectually interesting, but of little use for actual programming.

Contrasted with tree-recursion where the code is often so pithy that its correctness is obvious (assuming you understand recursion).

Re:Dijkstra was right – recursion should not be difficult by eru on Monday August 07, 2017 @07:08PM (#14952048)
Tail recursion sometimes comes up naturally. It's essentially a goto with parameters. Thus it comes up where goto is also (at least borderline) useful, eg implementing state machines.
Re:Dijkstra was right – recursion should not be difficult by aidenn0 on Monday August 07, 2017 @07:42PM (#14952237)
I almost added a parenthetical after "rarely" to the effect that state-machines are the one exception I've encountered.
Re:Dijkstra was right – recursion should not be difficult by Jtsummers on Monday August 07, 2017 @03:16PM (#14950350)
This structure doesn't prevent tail-call recursion. It's a good template for most instances of recursion. To modify the GP's version, you'd just need to add an accumulator parameter to the function (or to some higher scope, global or class) which could be modified and tested on each recursive call.
Re:Dijkstra was right – recursion should not be difficult by tome on Wednesday August 09, 2017 @04:12AM (#14966512)
Technically you can convert to a tail-call form (such as CPS) but regardless, you can't get away with consuming O(d) space where d is the depth of the tree.
Re:Dijkstra was right – recursion should not be difficult by Jtsummers on Wednesday August 09, 2017 @06:45AM (#14967274)
Not necessarily. The form the OP posted is fine for most recursive calls and won't consume large amounts of memory unless the iterative equivalent would as well if you put it in tail call form:

  function f(n,acc) {
    if(n == 0) return acc;
    else return f(n-1,acc*n);
  }
  function fact(n) {
    return f(n,acc);
  }
That's exactly the structure the OP suggested and in this case won't use O(d) space if tail-call elimination is available in the language. Now, if the accumulator is growing a list result like map or some tree traversal, sure. It'll use O(d) memory. But so will the iterative equivalent.
Re:Dijkstra was right – recursion should not be difficult by tome on Wednesday August 09, 2017 @06:52AM (#14967326)
I disagree, because a has two recursive calls. In that case you can't make both calls to a into tail calls.
Re:Dijkstra was right – recursion should not be difficult by Jtsummers on Wednesday August 09, 2017 @07:30AM (#14967513)
Well, if you're hung up on the OP's code, they do the same thing twice so we could maybe just double the result when we put it into the accumulator.

If you read what I wrote I've qualified my statements properly. Not everything in the OP's form will be able to be made tail recursive. But the form they presented does not bar tail recursion and efficienct tail recursion at that.

Anything that can't be made efficient recursively would likely also use gobs of memory and time if constructed iteratively.