# Recursive array

**URL:** <https://www.ruby-forum.com/t/recursive-array/118021>\
**Category:** Ruby\
**Created:** [October 23, 2007, 11:42am UTC](https://www.ruby-forum.com/t/recursive-array/118021 "2007-10-23T11:42:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 11:42am UTC](https://www.ruby-forum.com/t/recursive-array/118021/1 "2007-10-23T11:42:49Z")

</div>

207:0\> a = [1,2,3,4]  
[1, 2, 3, 4]  
208:0\> a[4] = a  
[1, 2, 3, 4, […]]  
209:0\> a[4]  
[1, 2, 3, 4, […]]  
210:0\> a[4][4]  
[1, 2, 3, 4, […]]  
211:0\> a[4][4][4][4][4][3]  
4

have you ever used this technique? can you provide some example code  
for us which illustrates any kind of usefulness of this that you can  
imagine?

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 11:42am UTC](https://www.ruby-forum.com/t/recursive-array/118021/2 "2007-10-23T11:42:49Z")

</div>

also, I’m interested in what is going on “behind the scenes” here. is  
a[4][4][4][4][0] actually reaching in 4 levels deep?

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 11:45am UTC](https://www.ruby-forum.com/t/recursive-array/118021/3 "2007-10-23T11:45:45Z")

</div>

From: Simon S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 207:0\> a = [1,2,3,4]

# [1, 2, 3, 4]

# 208:0\> a[4] = a

# [1, 2, 3, 4, […]]

# 209:0\> a[4]

# [1, 2, 3, 4, […]]

# 210:0\> a[4][4]

# [1, 2, 3, 4, […]]

# 211:0\> a[4][4][4][4][4][3]

# 4

# 

# have you ever used this technique? can you

# provide some example code for us which illustrates

# any kind of usefulness of this that you can

# imagine?

i am not yet in graphs/game theory but recursion things are good for  
research in those fields 🙂

this is just one stupid example,

> def sumb(a,i)  
> return 0 if a[i]==a  
> a[i]+sumb(a,i+1)  
> end  
> =\> nil  
> irb(main):042:0\> a  
> =\> [1, 2, 3, 4, […]]  
> irb(main):043:0\> sumb(a,0)  
> =\> 10

i’m just playing w ruby 🙂

kind regards -botp

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 11:53am UTC](https://www.ruby-forum.com/t/recursive-array/118021/4 "2007-10-23T11:53:58Z")

</div>

Dido S. wrote:

> There are many uses for data structures of this type, as formal  
> computer science is rife with graphs and the algorithms to deal with  
> them. For instance, you could use such a structure as the internal  
> representation of a state machine of some kind (finite automaton,

Just for fun…

# alphabet = {0, 1, 2}

# language = (012)\*

s0 = []  
s1 = []  
s2 = []  
s\_ = []

fsm = [s0, s1, s2, s\_]

s0[0] = s1  
s0[1] = s\_  
s0[2] = s\_

s1[0] = s\_  
s1[1] = s2  
s1[2] = s\_

s2[0] = s\_  
s2[1] = s\_  
s2[2] = s0

s\_[0] = s\_  
s\_[1] = s\_  
s\_[2] = s\_

accept = proc do |string|  
string.split("").inject(s0) {|s, c| s[c.to\_i]}.equal? s0  
end

p accept[“012012”]  
p accept[""]  
p accept[“01201”]  
p accept[“12012”]  
p accept[“01201”]

p fsm # I love this part!

**END**

Output:

true  
true  
false  
false  
false  
[[[[[…], […], […]], [[[…], […], […]], [[…], […], […]],  
[…]], [[…], […], […]]], [[…], […], […]], [[…], […],  
[…]]], [[[…], […], […]], [[[…], […], […]], [[…], […],  
[…]], [[…], [[…], […], […]], [[…], […], […]]]], [[…],  
[…], […]]], [[[…], […], […]], [[…], […], […]], [[[[…],  
[…], […]], […], [[…], […], […]]], [[…], […], […]],  
[[…], […], […]]]], [[…], […], […]]]

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 11:51am UTC](https://www.ruby-forum.com/t/recursive-array/118021/5 "2007-10-23T11:51:37Z")

</div>

On 10/23/07, Simon S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> also, I’m interested in what is going on “behind the scenes” here. is  
> a[4][4][4][4][0] actually reaching in 4 levels deep?

Well, what seems to have happened here is you created what amounts to  
a cyclic graph using the array. Every time you get the 4th index of  
a, you get back a itself. It’s not exactly reaching in four levels  
deep but more accurately referencing itself four times. It’s as if you  
created an array of pointers in C, and had some element in the array  
be a pointer back to the array itself.

There are many uses for data structures of this type, as formal  
computer science is rife with graphs and the algorithms to deal with  
them. For instance, you could use such a structure as the internal  
representation of a state machine of some kind (finite automaton,  
pushdown automaton, linear bounded automaton, or even a Turing  
machine) , with each state being represented by an array, the inputs  
being indexes into the array, and the values of the array the new  
states the machine enters when a particular input is received. This is  
the simplest example I could think up off the top of my head, but read  
any decent book on algorithms and data structures and you’ll find  
hundreds more.

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/recursive-array/118021/6 "2007-10-23T12:09:47Z")

</div>

> [[[[[…], […], […]], [[[…], […], […]], [[…], […], […]],  
> […]], [[…], […], […]]], [[…], […], […]], [[…], […],  
> […]]], [[[…], […], […]], [[[…], […], […]], [[…], […],  
> […]], [[…], [[…], […], […]], [[…], […], […]]]], [[…],  
> […], […]]], [[[…], […], […]], [[…], […], […]], [[[[…],  
> […], […]], […], [[…], […], […]]], [[…], […], […]],  
> [[…], […], […]]]], [[…], […], […]]]

so… is this practical because it’s fucking awesome? or not practical  
because I’m not making money from it? either way, thanks 😃

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 23, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/recursive-array/118021/7 "2007-10-23T12:13:54Z")

</div>

On 23 Oct 2007, at 13:08, Simon S. wrote:

> also, I’m interested in what is going on “behind the scenes” here. is  
> a[4][4][4][4][0] actually reaching in 4 levels deep?

It calls Array#[] 5 times if that’s what you mean? But all on the  
same Array. It’s turtles all the way down would be another way of  
putting it (or one turtle repeated over and over again).

[alexg@powerbook]/Users/alexg/Desktop(31): cat test.rb  
a = [1,2,3,4]  
a[4] = a

set\_trace\_func proc {|event,file,line,id,binding,classname|  
printf “%8s %s:%-2d %10s %8s\n”,event,file,line,id,classname  
if event == “c-call”  
}

a[4][4][4][4][0]  
[alexg@powerbook]/Users/alexg/Desktop(32): ruby test.rb  
c-call test.rb:8 [] Array  
c-call test.rb:8 [] Array  
c-call test.rb:8 [] Array  
c-call test.rb:8 [] Array  
c-call test.rb:8 [] Array

> > 4
> > 
> > have you ever used this technique? can you provide some example code  
> > for us which illustrates any kind of usefulness of this that you can  
> > imagine?

I have to say I can’t imagine any practical use (obfuscation  
perhaps?), but maybe there are some fun games you can play with it.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 24, 2007, 1:58am UTC](https://www.ruby-forum.com/t/recursive-array/118021/8 "2007-10-24T01:58:17Z")

</div>

could you provide some code to exemplify this?

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 24, 2007, 2:13am UTC](https://www.ruby-forum.com/t/recursive-array/118021/9 "2007-10-24T02:13:18Z")

</div>

Eric P. wrote:

> I’m impressed, but the name “FSM” raised my expectations.  
> Is there any way you could construct an FSM, or render it,  
> so its output looks more like the Flying Spaghetti Monster?

Well, if you use the object\_ids, you can construct a graph.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 24, 2007, 1:50am UTC](https://www.ruby-forum.com/t/recursive-array/118021/10 "2007-10-24T01:50:42Z")

</div>

I’m impressed, but the name “FSM” raised my expectations.  
Is there any way you could construct an FSM, or render it,  
so its output looks more like the Flying Spaghetti Monster?

- Eric

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 24, 2007, 7:53am UTC](https://www.ruby-forum.com/t/recursive-array/118021/11 "2007-10-24T07:53:42Z")

</div>

Eric P. wrote:

> I’m impressed, but the name “FSM” raised my expectations.  
> Is there any way you could construct an FSM, or render it,  
> so its output looks more like the Flying Spaghetti Monster?

This can be a very first step into readability…

class Array  
attr\_accessor :my\_state\_name  
alias old\_inspect inspect  
def inspect  
if self.my\_state\_name  
(’ /’ + self.my\_state\_name + ‘/:’ +  
old\_inspect).gsub(/(]\s\*,)\s\*/, “\1\n”)  
else  
old\_inspect  
end  
end  
end

s0 = []  
s1 = []  
s2 = []  
s\_ = []

s0.my\_state\_name = “Start-End State S0”  
s1.my\_state\_name = “State S1”  
s2.my\_state\_name = “State S2”  
s\_.my\_state\_name = “Error State S\_”

fsm = [s0, s1, s2, s\_]

#… rest of program …

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [October 24, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/recursive-array/118021/12 "2007-10-24T19:41:33Z")

</div>

Eric P. wrote:

> I’m impressed, but the name “FSM” raised my expectations.  
> Is there any way you could construct an FSM, or render it,  
> so its output looks more like the Flying Spaghetti Monster?

I’m afraid I have not been touched by the noodly appendage, and I only  
write FSC, Flying Spaghetti Code. 😉
