Skip to content

Instantly share code, notes, and snippets.

@lion137
Created July 6, 2019 12:16
Show Gist options
  • Save lion137/de17c27ac1be999810bd498feabf9f8c to your computer and use it in GitHub Desktop.
Save lion137/de17c27ac1be999810bd498feabf9f8c to your computer and use it in GitHub Desktop.
python list
class Nil:
def is_empty(self):
return True
def head(self):
raise Exception("Exception: Empty list")
def tail(self):
raise Exception("Exception: Empty list")
class Cons:
def __init__(self, _head, _list_tail=Nil()):
self.head = _head
self.tail = _list_tail
def is_empty(self):
return False
def head():
return self.head
def tail():
return self.head
def __getitem__(self, index):
return nth(index, self)
class ImmutableList(metaclass=ABCMeta):
@abstractmethod
def is_empty(self):
pass
def head(self):
pass
def tail(self):
pass
ImmutableList.register(Nil);
ImmutableList.register(Cons)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment