Eugene
Eugene

Reputation: 89

Nil is not the empty list?

I am learning Scheme and am using an online interpreter from repl.it. I am having some trouble understanding why this returns #f:

(eq? '() 'nil) ; ==> #f

Upvotes: 5

Views: 1392

Answers (1)

Sylwester
Sylwester

Reputation: 48745

In LISP 1.5 nil, 'nil, '() and () evaluates to nil. The reader translated () and nil into the same singleton empty list and it is self evaluating so that the quote is optional. nil is also the only false value and all other values are truthy. The very first Scheme implementation was hosted by a commercial Lisp compatible with 1.5 and much of the host were used transparently in the first Scheme. Common Lisp is a descendant of 1.5 and still works like this, but Scheme has had changes from report to report and these are the rules from R5RS and later:

The empty list is '(). It's quoted and evaluates to (). It is not self evaluating so () is invalid Scheme code. 'nil evalutes to nil which is a symbol and not the empty list. In Scheme the only false value is #f and thus the empty list is truthy.

In code:

'()            ; ==> () 
'(1 . (2 . ()) ; ==> (1 2)
()             ; ==>  ERROR: Illegal empty application (not allowed)
(eq? '() 'nil) ; ==> #f (empty list and nil are different values)
(if '() #t #f) ; ==> #t (empty list is truthy)

Upvotes: 6

Related Questions