Linked lists (was Re: Typing system vs. Java

xtian at xtian at
Wed Aug 8 01:43:56 CEST 2001

On 07 Aug 2001 12:29:03 -0400, phawkins at wrote:
>>>>>> "AM" == Alex Martelli <aleaxit at> 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.

No ants, no pants.

More information about the Python-list mailing list