Python and Lisp : car and cdr
Terry Reedy
tjreedy at udel.edu
Sun Jun 19 12:20:32 EDT 2011
On 6/19/2011 9:24 AM, Steven D'Aprano wrote:
> No. Each cell in a Lisp-style linked list has exactly two elements, and
> in Python are usually implemented as nested tuples:
>
> (head, tail) # Annoyingly, this is also known as (car, cdr).
>
> where head is the data value and tail is either another Lisp-style list
> or a marker for empty (such as the empty tuple () or None).
>
> So a one-element linked list might be given as:
>
> (42, None)
>
> A two element list: (42, (43, None))
> Three element list: (42, (43, (44, None)))
>
> and so forth. So while you could harmlessly use a slice L[1:], there is
> no point, since L[1:] will have at most a single element.
It should be noted that the head element of any 'list' can also be a
'list' (as with Python lists),
t = { { (1,None), (2,(3,None)) ), ( (4,(5,None)), (6,None) ) )
so that the structure is actually a tree, which is a much more general
data structure than a true sequence of atoms. But TREP (for
tree-processing) is not as catchy as LISP (for list processing).
--
Terry Jan Reedy
More information about the Python-list
mailing list