Linked lists (was Re: Typing system vs. Java
xtian at hyperactive.co.nz
xtian at hyperactive.co.nz
Tue Aug 7 19:43:56 EDT 2001
On 07 Aug 2001 12:29:03 -0400, phawkins at spamnotconnact.com wrote:
>>>>>> "AM" == Alex Martelli <aleaxit at yahoo.com> writes:
>
>AM> def cdr(alist):
>AM> return alist[1]
>
>Make that:
>
>return alist[1:]
Nope, he definitely meant alist[1] - if you look at cons, it
constructs a (car, cdr) tuple. Which really does do the same thing as
a linked list - the list is a pair of (head, tail) where tail is (a
reference to) another linked list.
def cons(car, cdr):
return car, cdr
x = cons(1, ())
x -> (1, ())
x = cons(2, x)
x -> (2, (1, ()))
(You'd have to do the proper cons(1, ()) to start it off - otherwise I
guess it'd be a dotted pair.)
That's pretty spectacularly neat.
Nice one, Alex.
xtian
No ants, no pants.
More information about the Python-list
mailing list