4

I'm reading the book called the little schemer.

Before read that, i've finished reading first three chapter of SICP.

My question is that why second argument to cons must be a list.

However, (cons a b) works for all values a and b and

(car (cons a b)) = a

(cdr (cons a b)) = b

ray
  • 145
  • 9
  • 1
    It's just a Scheme/Lisp convention that a list is either nil, or a `cons` whose `cdr` is again a list. – Fred Foo Nov 26 '12 at 14:52

2 Answers2

4

The second argument to cons is not necessarily a list. It's a list only if you're, well, building a list (proper or otherwise). It's perfectly valid if the cdr part of a cons cell is not a list, for example, when building an association list:

(define lookup-table (list (cons 'x 10) (cons 'y 20) (cons 'z 30)))
(assoc 'z lookup-table)
=> '(z . 30)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

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

user825628
  • 179
  • 1
  • 2
  • 5