# Run: # python List.py # # Python doesn't have a linked list type built in, the way it has # arrays (which are called lists), dictionaries, and tuples. So, # let's build our own. # Make a new linked list. The r must be emptyList or another cons # value. def cons(f, r): # We choose to implement lists internally with tuples. # We could have made a class, used dictionaries, or arrays return (f, r) def first(L): # This is how to dereference the first value in a tuple return L[0] def rest(L): return L[1] def isLL(L): return (L == None) or (type(L) == type(())) # Some trivial examples: print first(cons(1, None)) print rest(cons(1, None)) L = cons("Hello", cons("World", None)) print first(L) print first(rest(L))