Python and Lisp : car and cdr

Teemu Likonen tlikonen at iki.fi
Sun Jun 19 12:38:03 EDT 2011


* 2011-06-19T12:20:32-04:00 * Terry Reedy wrote:

> 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).

> It should be noted that the head element of any 'list' can also be a
> list' (as with Python lists),

Both the head and tail elements of a cons cell can refer to any Lisp
objects. Cons cell is a general-purpose primitive data type but it is
_often_ used to build lists and trees so the tail element often refers
to another cons cell (or nil that terminates the list).

Let's not forget that Lisp's program code itself is built on such trees
of cons cells. Lisp code itself is represented in this primitive Lisp
data type. That's why Lisp is so powerful in meta programming. It's
trivial to use Lisp functions to create Lisp code.



More information about the Python-list mailing list