On Mon, 14 Mar 2011 04:49:44 +0800
Ana Zgombic <morse...@nybl.info> wrote:

> Hi list,
> i don't dig dotted pairs. it's hard for me to reconcile the existence
> of a dotted pair when there's a more general representation of it (a
> plain list).
> like in the design of the (tag) API for xhtml generation. why use a
> dotted pair instead of a two-element list?
> searching the net for examples and meanings doesn't give much.
> any insights on this?
This is something funny in lisp, dotted pairs, even though the syntax
is more complex, is actually the simplest non-atomic data structure in
picolisp, the list systax is actually a shortcut for a string of dotted
pairs: (1 2 3) is the same as (1 . (2 . (3 . NIL)), you can test that
writing the long version in the picolisp repl (interactive
interpreter), both versions will print (1 2 3), because they are the

The dotted pair is also called "cons cell", it's the building block for
more complex lisp data structures, like lists or trees. Each cons cell
has two parts, it's CAR and CDR (they used to be acronyms that used to
mean something, but now they are just old relics) In a list, cons cells
are chained like cars in a train, they usually have their "payload" in
the CAR (like trains) and a pointer to the next cell in the list in
it's CDR, going back to the (1 2 3) example:
 * Let's see the first cell we see it has the number one in it's CAR,
   and a cell in it's CDR.=20

 * that second cell has a number in it's CAR and another cell in it's
   CDR, and so on 'till the last one, where it has a special value
   "NIL", NIL literally means "Nothing", so our lisp machine reads it
   as "Nothing follows"

        +---+---+  +---+---+  +---+---+
        | 1 |  --->| 2 |  --->| 3 |  ---> NIL
        +---+---+  +---+---+  +---+---+

Kinda looks like a train, huh? (if it doesn't you need to read this
with fixed-width font)

Now, what if instead of nil, we link the last CDR to the first cell?

        +---+---+  +---+---+  +---+---+
     ,->| 1 |  --->| 2 |  --->| 3 |  ----,
     |  +---+---+  +---+---+  +---+---+  |

We got a circular list!, the syntax to write such a thing in picolisp
is (1 2 . 3)

And what is the empty list in picolisp? NIL, yes, NIL is a pretty
special beast, it's not only false, an empty symbol, it's also an empty
list! you can call him NIL: The king of Nothing-ham.

You can also link other cells in other shapes, putting cells in CARs
and CDRs to form some pretty data structures like trees or other graphs.

Better described here http://en.wikipedia.org/wiki/Cons#Lists

> thank you.
> Ana
> PS:
> i don't mean to test everybody's patience with my questions. i'm
> really just learning. :)

LOL i cant believe i ended up writing that wall of text, i hope it
helps :3

UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to