Why the second argument against cons should be a list

I am reading a book called a little intriguer .

Before reading this, I ended up reading the first three chapters of SICP .

My question is why the second cons argument should be a list .

However (cons ab) works for all values ​​of a and b and

(car (cons ab)) = a

(cdr (cons ab)) = b

+4
source share
2 answers

The second argument to cons not necessarily a list. This is a list only if you, well, create a list (correct or different). This is great if the cdr part of the cons cell is not a list, for example, when creating a list of associations:

 (define lookup-table (list (cons 'x 10) (cons 'y 20) (cons 'z 30))) (assoc 'z lookup-table) => '(z . 30) 
+4
source

Not all Lisp implementations allow the absence of a list as the second argument to cons. For example, see https://scheme.cs61a.org/

0
source

All Articles