Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- empty ='empty'
- four = [1, [2, [3, [4, 'empty']]]]
- def is_link(s):
- """s is a linked list if it is empty or a (first, rest) pair."""
- return s == empty or (len(s) == 2 and is_link(s[1]))
- def link(first, rest):
- """Construct a linked list from its first element and the rest."""
- assert is_link(rest), "rest must be a linked list."
- return [first, rest]
- def first(s):
- """Return the first element of a linked list s."""
- assert is_link(s), "first only applies to linked lists."
- assert s != empty, "empty linked list has no first element."
- return s[0]
- def rest(s):
- """Return the rest of the elements of a linked list s."""
- assert is_link(s), "rest only applies to linked lists."
- assert s != empty, "empty linked list has no rest."
- return s[1]
- four = link(1, link(2, link(3, link(4, empty))))
- def len_link(s):
- """Return the length of linked list s."""
- length = 0
- while s != empty:
- s, length = rest(s), length + 1
- return length
- def getitem_link(s, i):
- """Return the element at index i of linked list s."""
- while i > 0:
- s, i = rest(s), i - 1
- return first(s)
- def extend_link(s, t):
- """Return a list with the elements of s followed by those of t."""
- assert is_link(s) and is_link(t)
- if s == empty:
- return t
- else:
- return link(first(s), extend_link(rest(s), t))
- def apply_to_all_link(f, s):
- """Apply f to each element of s."""
- assert is_link(s)
- if s == empty:
- return s
- else:
- return link(f(first(s)), apply_to_all_link(f, rest(s)))
- def keep_if_link(f, s):
- """Return a list with elements of s for which f(e) is true."""
- assert is_link(s)
- if s == empty:
- return s
- else:
- kept = keep_if_link(f, rest(s))
- if f(first(s)):
- return link(first(s), kept)
- else:
- return kept
- def join_link(s, separator):
- """Return a string of all elements in s separated by separator."""
- if s == empty:
- return ""
- elif rest(s) == empty:
- return str(first(s))
- else:
- return str(first(s)) + separator + join_link(rest(s), separator)
- def mutable_link():
- """Return a functional implementation of a mutable linked list."""
- contents = empty
- def dispatch(message, value=None):
- nonlocal contents
- if message == 'len':
- return len_link(contents)
- elif message == 'getitem':
- return getitem_link(contents, value)
- elif message == 'push_first':
- contents = link(value, contents)
- elif message == 'pop_first':
- f = first(contents)
- contents = rest(contents)
- return f
- elif message == 'str':
- return join_link(contents, ", ")
- return dispatch
- def to_mutable_link(source):
- """Return a functional list with the same contents as source."""
- x = mutable_link()
- for element in reversed(source):
- x('push_first', element)
- return x
- suits = ['coin', 'string', 'myriad']
- s = to_mutable_link(suits)
- print(type(s))
- print(s('str'))
- print(s('pop_first'))
- print(s('str'))
Add Comment
Please, Sign In to add comment