Created
July 6, 2019 12:16
-
-
Save lion137/de17c27ac1be999810bd498feabf9f8c to your computer and use it in GitHub Desktop.
python list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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