Gonna have to keep this short because I'm very busy today. Today I'm reading about lists in Lisp. Lisp's take on lists is what allows it to use code as data and vice-versa. Only going to read the first section (excluding intro) of this chapter from PCL.
The first thing I learned was that lists in CL basically don't actually exist. They're a series of connected what are called "cons cells." These seem to basically be a map from one value to another. The first entry in a cons cell is called the car
and the second is called the cdr
. Cons cells are mutable too. So you can perform setf
on either the car
or the cdr
. So here's a code example:
(defparameter *sample-cons* (cons 20 "hello")) ; ==> (20 . "hello")
(setf (car *sample-cons*) 5)
*sample-cons* ; ==> (5 . "hello")
This makes a global cons cell variable with a car
of 20 and a cdr
of "hello". Then we change the car
to be 5.
Lists are made by linking cons cells together where the car
s contain list elements and the cdr
s are references to the next cons cell in the chain. The last cdr
will always be nil
. Sound familiar? It's just a singly-linked list.