List without nil in Lisp

I know that in Lisp the list should end with nil, but an expression like

(print (cons 1 (cons 3 2))) 

does not cause errors. He prints:

 (1 3 . 2) 

Is it correct?

I am using GNU Clisp.

+7
list null lisp
source share
4 answers

You have a dotted list , which is a kind of invalid list .

The chain of CONS cells where the last CDR is NIL is the correct list .

+9
source share

In Lisp, the correct list ends in NIL , but you also have invalid lists. One kind of invalid list is a list in which the last cons cell contains an atom other than NIL in its CDR . (1 3 . 2) - This is exactly the wrong list.

You may even have incorrect lists in which it does not have the last cell at all. CAR and CDR are basically just pointers, so you can have circular lists!

In Common Lisp (which CLISP implements), many standard functions will not work with invalid lists as arguments.

+11
source share

It is also interesting to note what happens when evaluating the correct lists:

 ;; A proper list (cons '+ (cons 5 (cons 10 '()))) ⇒ (+ 5 10) (eval (+ 5 10)) ⇒ 15 

compared to dashed lists:

 ;; A dotted list (cons '+ (cons 5 (cons 10 5000))) ⇒ (+ 5 10 . 5000) (eval (+ 5 10 . 5000)) ⇒ 15 

It ignores the final atom.

+3
source share

When you iterate over a list that, as you know, reached the end when you typed zero. You have a list with a car and a pair point.

-one
source share

All Articles