# How do Racket's basic list functions compare to Python? # Suppose I have this list: my_list = [1, 2, 3, 4] # Here are the two basic accessors: def first(a_list): # aka 'car' return a_list[0] def rest(a_list): # aka 'cdr' return a_list[1:] print( first(my_list) ) print( rest(my_list) ) # Racket provides named list accessors for the first ten items in # a list: first, second, ... tenth. It also provides a position- # based function, list-ref, for accessing list items as if they # were in an array. # # In Python, lists *are* variable-length arrays. # In Racket, lists are linked lists. # What about cons? Python makes it easy to add items to the end: # my_list.append(0) # # In Racket, we add to the front of a list, not the back. # Here is cons: def cons(item, a_list): return [item] + a_list print( cons(0, my_list) ) # In Python, we might also use # a_list.insert(0, item) # but that *changes* a_list. cons does not change its list!