Search code examples
schemelispracketsicp

Creating lists in various ways


I can create the single-element list (2) in all of the following ways:

'(2)      
(list 2)  
(cons 2 nil)

However, why does the following give an error?

(2 . nil)

Is there no such thing as the "scalar null" value in scheme? Or is there an alternate way to do that?


Solution

  • (2 . '()) does indeed represent the singleton list '(2): the latter is shorthand for the former. The problem is that, if you use it in the same context that you would use (cons 2 '()) in, you get an error. Why? The same error you would get if you tried to evaluate (2)! That means to call 2 as a function, passing it no arguments.

    If instead you want to just create such a list, without calling it, you do the same thing you would with the shorthand: quote it.

    '(2 . ())