Ruby supports recursive arrays (that is, self-containing arrays):
a = [] # => [] a << a # => [[...]] a.first == a # => true
This is intrinsically cool, but what work can you do with it?
When should I use recursion? Recursion is made for solving problems that can be broken down into smaller, repetitive problems. It is especially good for working on things that have many possible branches and are too complex for an iterative approach . One good example of this would be searching through a file system.
If you have a problem that is too complex, you can use recursion to break it down into simpler blocks. You do this in real life all the time. Imagine you have a whole box full of $100 bills and you need to count how much money you have.
While iterative functions can usually do the same job, recursive functions are simpler to read and understand. Recursive functions are especially powerful in traversing tree structures. Another reason that recursion is important to understand is that many algorithms use recursion.
In computer science, recursion is a method of solving a computational problem where the solution depends on solutions to smaller instances of the same problem. Recursion solves such recursive problems by using functions that call themselves from within their own code.
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
While recursion is not an appropriate method for every case, it is an important concept to understand. Programming involves recursive thinking, and it can help us to write shorter and more efficient code when used appropriately. While iterative functions can usually do the same job, recursive functions are simpler to read and understand.
Programming involves recursive thinking, and it can help us to write shorter and more efficient code when used appropriately. While iterative functions can usually do the same job, recursive functions are simpler to read and understand. Recursive functions are especially powerful in traversing tree structures.
One critical requirement of recursive functions is the termination point or base case. Every recursive program must have a base case to make sure that the function will terminate. Missing base case results in unexpected behavior.
A directed graph with undifferentiated edges could have each vertex represented simply as an array of the the vertices reachable from that vertex. If the graph had cycles, you would have a 'recursive array', especially if an edge could lead back to the same vertex.
For example, this graph:
...could be represented in code as:
nodes = { a:[], b:[], c:[], d:[] } nodes[:a] << nodes[:a] nodes[:a] << nodes[:b] nodes[:b] << nodes[:a] nodes[:b] << nodes[:c] p nodes #=> {:a=>[[[...], []], [...]], :b=>[[[...], [...]], []], :c=>[], :d=>[]}
Usually the representation of each vertex would be more 'robust' (e.g. a class instance with properties for the name and array of outgoing edges), but it's not impossible to imagine a case where you wanted a very lightweight representation of your data (for very large graphs) and so needed to use a minimal representation like this.
Ruby supports recursive arrays
To me the question is why should it not support it?
An Array is simply a collection of references. Should it check each element and throw an error if one of the refers to the collection itself, so prevent recursion or using it for graphs like Phrogz' example.
So I don't think it's a feature, but if it would be, most languages I know has it, even Java.. Just use Object as Array elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With