These are chat archives for evhub/coconut

6th
Oct 2017
Hanyuan Li
@hanyuone
Oct 06 2017 00:05 UTC
Hello! Is anyone on?
How would I implement a singly-linked list in Coconut using data?
Evan Hubinger
@evhub
Oct 06 2017 02:13 UTC

@Qwerp-Derp How about something like this:

data Node(elem, next_node)

def linked_list([head] :: tail) =
    Node(head, linked_list(tail))
@addpattern(linked_list)
def linked_list((||)) =
    None

def ll_iter(None) = (||)
@addpattern(ll_iter)
def ll_iter(Node(elem, next_node)):
    yield elem
    yield from ll_iter(next_node)

test_ll = range(5) |> linked_list
print(test_ll)
print(test_ll |> ll_iter |> list)

It's certainly not efficient, though if you want efficiency you should probably just use collections.deque.