Notification texts go here Contact Us Buy Now!

Why does (nil . nil) evaluate to (nil) instead of just nil in SBCL?

Why does (nil . nil) evaluate to (nil) instead of just nil in SBCL?

This behavior is not specific to SBCL; it's defined in the Common Lisp standard and applies to all compliant implementations.

In Common Lisp, the empty list is represented by the symbol NIL or an empty pair of parentheses (). They are the same object, as can be verified with (eq 'nil '()), which returns T.

'NIL is not a cons cell, cons cells are data structures that form linked lists in Common Lisp, it's a special value that represents the empty list. The CAR and CDR functions, used to access the first and rest of a cons cell respectively, are defined to return NIL when given NIL as an argument. This is a special case to ensure consistent behavior when working with lists.

The dotted pair notation, (NIL . NIL), is a way to represent a cons cell where both the CAR and CDR are NIL. However, this notation is equivalent to (NIL) in Common Lisp, as the trailing NIL is omitted when printing or evaluating the cons cell.

The following examples illustrate this behavior:

(car '()) == '() (cdr '()) => NIL

Here, (car '()) and (cdr '()) return NIL because they are called with an empty list, which is represented by '().

(car '(NIL . NIL)) => NIL (cdr '(NIL . NIL)) => NIL

In this case, (car '(NIL . NIL)) and (cdr '(NIL . NIL)) also return NIL because '(NIL . NIL) is equivalent to (NIL), which is an empty list.

To summarize, (NIL . NIL) evaluates to (NIL) in Common Lisp because the dotted pair notation is a way to represent a cons cell where both the CAR and CDR are NIL, but this representation is equivalent to the empty list NIL.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.