News for hackers. Stuff that matters.
Re:Thinking About Recursion by brianberns on Wednesday March 08, 2017 @07:01PM (#13825215)
Many commonly-used data structures (e.g. trees) are nearly impossible to manage without recursion.
Re:Thinking About Recursion by crimsonalucard on Wednesday March 08, 2017 @07:31PM (#13825464)
Not true. A loop and a stack can achieve the same thing!
Re:Thinking About Recursion by auxym on Wednesday March 08, 2017 @08:16PM (#13825850)
As an amateur programmer, I'm intrigued in how this is achieved, would you happen to have any references you could point me to?

My first encounter with recursion was when I needed to traverse a directory tree. I couldn't figure how to go arbritrarily deep without writing tens of nested for-loops. Then recursion somehow hit me, or I found something interesting on Google, can't remember, it was over 10 years ago, pretty sure SO wasn't around.

...and then a while later I found out about os.walk (this was python).

Re:Thinking About Recursion by thethirdone on Wednesday March 08, 2017 @11:01PM (#13826866)
http://blog.moertel.com/ does a decent job of explaining some practical aspects for de-recursivying, but I'll give you a basic explanation of that plus a more theoretical answer showing that any computatble function can be written using only while loops and a stack.

Practical: If we right all of are programs and tail-recursive (returning a recursive call or constant at the end of the prgram) programs they are easy to transform into simple while loops with no stack. The idea is instead of calling another function just restart the current one with the updated arguments.

EX:

    f(n,x)=f(n-1,x+1)
    f(0,x)=x
goes to

    while(n > 0){
      n--;
      x++
    }
Other techniques such as trampolining and continuation passing style also help translate recursive functions into iterative ones.

Theorectical: Think about Brainfuck or Turing machines, these computing frameworks can compute any computable function using only what amounts to while loops and infinite memory.

Given then that we can produce arbitrary control flow using only while loops, we can reproduce how recursive calls are computed in computers with stack frames (provided by our stack).

Re:Thinking About Recursion by clusmore on Wednesday March 08, 2017 @09:07PM (#13826229)
Here's a recursive directory traversal in Python:

  import os
  import os.path
  
  def list_files_recursive(path):
      filenames = os.listdir(path)
      for filename in filenames:
          fullname = os.path.join(path, filename)
          if os.path.isdir(fullname):
              yield from list_files_recursive(fullname) # recursively explore folders
          else:
              yield fullname
and here is the same program with an explicit stack

  import os
  import os.path
  
  def list_files_stack(path):
      folders = [path] # create stack of unexplored folders
      while folders: # repeat until stack is empty
          path = folders.pop() # pick a folder from stack
          filenames = os.listdir(path)
          for filename in filenames:
              fullname = os.path.join(path, filename)
              if os.path.isdir(fullname):
                  folders.append(fullname) # push folder to stack
              else:
                  yield fullname
Re:Thinking About Recursion by auxym on Wednesday March 08, 2017 @09:52PM (#13826510)
Wow, somehow I expected something complex.

Thanks a lot!

Re:Thinking About Recursion by crimsonalucard on Thursday March 09, 2017 @01:39AM (#13827507)
Lets say you have two functions. Function A and function B. Function A does a bunch of stuff and function B is executed in the middle of function A. Well when function B returns the program must remember the context of function A in order to execute the remaining steps. It does this by storing the entire context of function A on a stack. This is known as the 'call stack.' Essentially, all recursive functions can and usually are implemented using a stack.
Re:Thinking About Recursion by vertex-four on Wednesday March 08, 2017 @07:44PM (#13825581)
Indeed. People who claim recursion is the only way to handle some types of data structures are missing how recursion is implemented. In practice, it boils down to a stack and a loop - and in the case of tail-recursive functions, not even the stack - though it may be a little prettier if you use recursion.
Re:Thinking About Recursion by charles-salvia on Wednesday March 08, 2017 @09:06PM (#13826227)
Recursion sometimes makes it easier to implement certain algorithms, since modern programming languages have an implicit call stack which conveniently provides a snapshot of the state of all local stack variables at that point in the algorithm.
Re:Thinking About Recursion by crimsonalucard on Thursday March 09, 2017 @01:41AM (#13827515)
Here's a mind bending thought. It is actually the loop that is syntactic sugar and recursion is the implementation.
Re:Thinking About Recursion by buzzybee on Thursday March 09, 2017 @04:26AM (#13828164)
You're begging the question of what the lambda calculus is. As a theoretical basis for computing it sits equivalently with Turing machines. In practical use, there are consequences with trying to represent the computation with one or the other model, and our physical hardware resembles neither, really.
Re:Thinking About Recursion by vertex-four on Thursday March 09, 2017 @06:13AM (#13828520)
In practice, lambda calculus machines are rare, and your code probably compiles down to machine code that more closely emulates a loop and a stack. I'm not saying they're not theoretically equivalent, of course.
Re:Thinking About Recursion by ihoz on Thursday March 09, 2017 @09:28AM (#13829453)
Recursion is nothing more than implicitly using a stack (via the call stack) or explicitly with a loop.

You just won't get a stack overflow exception with the loop -- you'll run out of heap instead. It's still the stack filling up.

Re:Thinking About Recursion by dreamcompiler on Thursday March 09, 2017 @12:51AM (#13827297)
For many algorithms you'll have to build and maintain a stack manually if you use a loop. I once saw an old Fortran routine that expressed Quicksort this way.