Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are recursive arrays good for?

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?

like image 446
hoffm Avatar asked May 15 '12 18:05

hoffm


People also ask

What is recursive good for?

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.

Why is recursion is important in our daily life?

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.

Why are recursive functions important?

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.

What is the objective of using recursive function?

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.

What is recursive algorithm?

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.

Why is recursion important in C++?

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.

Why do we use recursive functions in Python?

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.

What are the basic requirements of recursive functions?

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.


2 Answers

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:
directed cyclic 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.

like image 181
Phrogz Avatar answered Oct 10 '22 16:10

Phrogz


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.

like image 27
inger Avatar answered Oct 10 '22 17:10

inger