recursive array

S

Simon Schuster

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?
 
S

Simon Schuster

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?
 
A

Alex Gutteridge

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
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 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 Gutteridge

Bioinformatics Center
Kyoto University
 
P

Peña, Botp

RnJvbTogU2ltb24gU2NodXN0ZXIgW21haWx0bzpzaWduaWZpY2FudHNAZ21haWwuY29tXSANCiMg
MjA3OjA+IGEgPSBbMSwyLDMsNF0NCiMgWzEsIDIsIDMsIDRdDQojIDIwODowPiBhWzRdID0gYQ0K
IyBbMSwgMiwgMywgNCwgWy4uLl1dDQojIDIwOTowPiBhWzRdDQojIFsxLCAyLCAzLCA0LCBbLi4u
XV0NCiMgMjEwOjA+IGFbNF1bNF0NCiMgWzEsIDIsIDMsIDQsIFsuLi5dXQ0KIyAyMTE6MD4gYVs0
XVs0XVs0XVs0XVs0XVszXQ0KIyA0DQojIA0KIyBoYXZlIHlvdSBldmVyIHVzZWQgdGhpcyB0ZWNo
bmlxdWU/IGNhbiB5b3UNCiMgcHJvdmlkZSBzb21lIGV4YW1wbGUgY29kZSBmb3IgdXMgd2hpY2gg
aWxsdXN0cmF0ZXMgDQojIGFueSBraW5kIG9mIHVzZWZ1bG5lc3Mgb2YgdGhpcyB0aGF0IHlvdSBj
YW4NCiMgaW1hZ2luZT8NCg0KaSBhbSBub3QgeWV0IGluIGdyYXBocy9nYW1lIHRoZW9yeSBidXQg
cmVjdXJzaW9uIHRoaW5ncyBhcmUgZ29vZCBmb3IgcmVzZWFyY2ggaW4gdGhvc2UgZmllbGRzIDop
DQoNCnRoaXMgaXMganVzdCBvbmUgc3R1cGlkIGV4YW1wbGUsDQoNCj4gZGVmIHN1bWIoYSxpKQ0K
PiAgIHJldHVybiAwIGlmIGFbaV09PWENCj4gICBhW2ldK3N1bWIoYSxpKzEpDQo+IGVuZA0KPT4g
bmlsDQppcmIobWFpbik6MDQyOjA+IGENCj0+IFsxLCAyLCAzLCA0LCBbLi4uXV0NCmlyYihtYWlu
KTowNDM6MD4gc3VtYihhLDApDQo9PiAxMA0KDQppJ20ganVzdCBwbGF5aW5nIHcgcnVieSA6KQ0K
DQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
J

Joel VanderWerf

Dido said:
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,

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
[[[[[...], [...], [...]], [[[...], [...], [...]], [[...], [...], [...]],
[...]], [[...], [...], [...]]], [[...], [...], [...]], [[...], [...],
[...]]], [[[...], [...], [...]], [[[...], [...], [...]], [[...], [...],
[...]], [[...], [[...], [...], [...]], [[...], [...], [...]]]], [[...],
[...], [...]]], [[[...], [...], [...]], [[...], [...], [...]], [[[[...],
[...], [...]], [...], [[...], [...], [...]]], [[...], [...], [...]],
[[...], [...], [...]]]], [[...], [...], [...]]]
 
S

Simon Schuster

[[[[[...], [...], [...]], [[[...], [...], [...]], [[...], [...], [...]],
[...]], [[...], [...], [...]]], [[...], [...], [...]], [[...], [...],
[...]]], [[[...], [...], [...]], [[[...], [...], [...]], [[...], [...],
[...]], [[...], [[...], [...], [...]], [[...], [...], [...]]]], [[...],
[...], [...]]], [[[...], [...], [...]], [[...], [...], [...]], [[[[...],
[...], [...]], [...], [[...], [...], [...]]], [[...], [...], [...]],
[[...], [...], [...]]]], [[...], [...], [...]]]

so... is this practical because it's fucking awesome? or not practical
because I'm not making money from it? either way, thanks :D
 
E

Eric Promislow

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
 
W

Wolfgang Nádasi-Donner

Eric said:
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
 
W

Wolfgang Nádasi-Donner

Eric said:
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
 
J

Joel VanderWerf

Eric said:
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. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,026
Latest member
DannieKeeg

Latest Threads

Top