4 ms·
Python lists are array-backed, Lisp lists are singly-linked lists. Appending to the front of an array list is O(n)--you don't want to do it. Appending to the ba
by samdk 14y ago
Python lists are array-backed, Lisp lists are singly-linked lists. Appending to the front of an array list is O(n)--you don't want to do it. Appending to the back is constant time, but is an in-place operation and doesn't return anything, which is less than ideal for FP purposes.
- chimeracoder 14y agoThat's the problem. I can fake a functional paradigm within Python in some settings, but it falls apart at the seams.
- groovy2shoes 14y agoYou can always use 2-tuples to get lists in the Lisp sense. You'd add an item to the head of a list by just pairing it: lst = (2, (3, ())) newlst = (1, lst) Toss in some small helper functions and they become less unwieldy. You could pretty easily write an iterator for processing these lists: class List: def __init__(self, lst): self.cursor = lst def __iter__(self): return self def next(self): if self.cursor == (): raise StopIteration else: head = self.cursor[0] self.cursor = self.cursor[1] return head Then you can do stuff like: >>> for i in List(newlst): ... print i ... 1 2 3 >>> map(lambda x: x + 1, List(newlst)) [2, 3, 4]