Guest User

Untitled

a guest
Sep 26th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. empty ='empty'
  2. four = [1, [2, [3, [4, 'empty']]]]
  3.  
  4.  
  5. def is_link(s):
  6.     """s is a linked list if it is empty or a (first, rest) pair."""
  7.     return s == empty or (len(s) == 2 and is_link(s[1]))
  8.  
  9. def link(first, rest):
  10.         """Construct a linked list from its first element and the rest."""
  11.         assert is_link(rest), "rest must be a linked list."
  12.         return [first, rest]
  13.  
  14. def first(s):
  15.         """Return the first element of a linked list s."""
  16.         assert is_link(s), "first only applies to linked lists."
  17.         assert s != empty, "empty linked list has no first element."
  18.         return s[0]
  19.  
  20. def rest(s):
  21.         """Return the rest of the elements of a linked list s."""
  22.         assert is_link(s), "rest only applies to linked lists."
  23.         assert s != empty, "empty linked list has no rest."
  24.         return s[1]
  25.  
  26. four = link(1, link(2, link(3, link(4, empty))))
  27.  
  28. def len_link(s):
  29.         """Return the length of linked list s."""
  30.         length = 0
  31.         while s != empty:
  32.             s, length = rest(s), length + 1
  33.         return length
  34.  
  35. def getitem_link(s, i):
  36.         """Return the element at index i of linked list s."""
  37.         while i > 0:
  38.             s, i = rest(s), i - 1
  39.         return first(s)
  40.  
  41. def extend_link(s, t):
  42.         """Return a list with the elements of s followed by those of t."""
  43.         assert is_link(s) and is_link(t)
  44.         if s == empty:
  45.             return t
  46.         else:
  47.             return link(first(s), extend_link(rest(s), t))
  48.        
  49. def apply_to_all_link(f, s):
  50.         """Apply f to each element of s."""
  51.         assert is_link(s)
  52.         if s == empty:
  53.             return s
  54.         else:
  55.             return link(f(first(s)), apply_to_all_link(f, rest(s)))
  56.  
  57. def keep_if_link(f, s):
  58.         """Return a list with elements of s for which f(e) is true."""
  59.         assert is_link(s)
  60.         if s == empty:
  61.             return s
  62.         else:
  63.             kept = keep_if_link(f, rest(s))
  64.             if f(first(s)):
  65.                 return link(first(s), kept)
  66.             else:
  67.                 return kept
  68.  
  69. def join_link(s, separator):
  70.         """Return a string of all elements in s separated by separator."""
  71.         if s == empty:
  72.             return ""
  73.         elif rest(s) == empty:
  74.             return str(first(s))
  75.         else:
  76.             return str(first(s)) + separator + join_link(rest(s), separator)
  77.  
  78.  
  79.  
  80. def mutable_link():
  81.         """Return a functional implementation of a mutable linked list."""
  82.         contents = empty
  83.         def dispatch(message, value=None):
  84.             nonlocal contents
  85.             if message == 'len':
  86.                 return len_link(contents)
  87.             elif message == 'getitem':
  88.                 return getitem_link(contents, value)
  89.             elif message == 'push_first':
  90.                 contents = link(value, contents)
  91.             elif message == 'pop_first':
  92.                 f = first(contents)
  93.                 contents = rest(contents)
  94.                 return f
  95.             elif message == 'str':
  96.                 return join_link(contents, ", ")
  97.         return dispatch
  98.  
  99. def to_mutable_link(source):
  100.         """Return a functional list with the same contents as source."""
  101.         x = mutable_link()
  102.         for element in reversed(source):
  103.             x('push_first', element)
  104.         return x
  105.  
  106. suits =  ['coin', 'string', 'myriad']
  107.  
  108. s = to_mutable_link(suits)
  109.  
  110. print(type(s))
  111. print(s('str'))
  112. print(s('pop_first'))
  113. print(s('str'))
Add Comment
Please, Sign In to add comment