Submitted by musou in programming
flabberghaster wrote
Reply to comment by musou in ruby lets you put an array inside itself by musou
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.
musou OP wrote
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 ._.
Viewing a single comment thread. View all comments