The reason that you receive an error when using
nth is because when two
atoms are
cons'd, the result is a dotted pair as opposed to a linked list. As such, when a dotted pair is supplied to the
nth function, since the decrement register contains a pointer to an atom rather than a pointer to the tail of the list, the
nth function will error; whereas, the
cdr function will successfully return either an atom (for the case of a dotted pair), or the tail of the list (for the case of a singly linked list).
Observe the following -
_$
(setq x
(list 1 2)) ;; Singly linked list(1 2)
_$
(car x
) ;; Contents of the address register returns atom1
_$
(cdr x
) ;; Contents of the decrement register returns tail of list(2)
_$
(nth 0 x
) ;; nth traverses the linked list a number of times equal to the index1
2
(1 . 2)
_$
(car y
) ;; Contents of the address register returns atom1
_$
(cdr y
) ;; Contents of the decrement register returns atom2
_$
(nth 0 y
) ;; nth traverses the linked list a number of times equal to the index1
_$
(nth 1 y
) ;; nth cannot traverse the list since the decrement register contains an atom --> error; error: bad list: 2