Submitted by musou in programming
irb(main):001:0> a = []; a<<a
=> [[...]]
i have tried for a long time to think of any way i could make this do anything useful, but so far i've got nothing
Submitted by musou in programming
irb(main):001:0> a = []; a<<a
=> [[...]]
i have tried for a long time to think of any way i could make this do anything useful, but so far i've got nothing
Turns out python can too. I was trying to think of how to get c++ to do it, but i could only think if how to do it using void pointers since to add something to a container that container would have to be a container of its own type which I can't think how to do off the top of my head.
Interesting problem.
it's cool to know that python can do it too. i guess i should make a list of which languages can and can't do it.
it's super fun to think about, but i still can't come up with any way in which i could exploit it to do something fun or useful in order to make straightlaced programmers care about it
I think that it's basically a special case of how in those languages it's easy to assign a reference to things. Adding a list to itself is not really that different than assigning a reference to an object as a member of itself. For instance in python you can do:
class Node:
__init__(self, value, next=None):
self.value = value
self.next = next
def main():
head = Node("last")
tail = head
for i in range(10): head = Node(i, head)
tail.next = head
And this creates a cyclic linked list. You could just as easily assign a = Node(9); a.next = a
and have the graph have only one vertex. It's the same principle with adding a list to itself: you're not actually adding the list to itself; rather you're putting a reference to the container, inside the container. Like writing down the location of a bank on a slip of paper, then putting that slip of paper inside a safe deposit box stored inside the same bank.
yeah! this is a good explanation of why it works. i'm mostly interested in figuring out ways to use recursive arrays that are practically useful, or at least interesting. but i still have nothing so far ._.
What you're actually doing in Python when you put a list into itself is to put a reference to the list into itself.
L = []
L.append(L)
L[0] is L # True
Python and Ruby arrays are heterogenous, unlike in C++, where strong typing forces us to define a common type for objects contained in the same collection. There's no way to do this with the STL <vector>
, but you could do this with an object system, for example Qt, where all "Q" objects derive from QObject:
QVector<QObject*> objectVector;
objectVector.append(&objectVector);
I actually run into these types of circular reference loops often in JS, especially when I want to serialize arbitrary objects to JSON. Some other formats such as YAML provide reference semantics and are thusly able to deal with circular references, but JSON does not.
does the set of all sets contain itself?
i'll let you know when i finish .add
ing every set to this Set
i just instantiated
Moonside wrote
scary