class Link(object): empty = () def __init__(self, first, rest=empty): self.first = first self.rest = rest def__len__(self): return 1 + len(self.rest) def __getitem__(self, i): if i == 0: return self.first return self.rest[i - 1] def __repr__(self): if self.rest is empty: return 'Link({})'.format(repr(self.first)) return 'Link({}, {})'.format(repr(self.first), repr(self.rest)) def seq_to_link(seq): new = Link.empty for elem in seq[::-1]: new = Link(elem, new) return new seq_to_link([1, 2, 3, 4])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question