This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

correct behaviour of nconc

3 posts · 4006 views

Hi guys,
I'm currently implementing my own lisp interpreter. At the moment I working on the function nconc. I'm not sure how it should behave for the following cases:
(nconc 1)
or
(nconc nil 1)
The gnu common lisp compiler returns 1 for both cases. Shouldn't the return value be a list. Isn't throwing an exception the better behaviour? Or is this behaviour forced by the standard?

regards,
Martin Kalbfuß

Re: correct behaviour of nconc

By the description section in CLHS you apply 2nd and 3rd rules.
[2] (nconc nil . lists) == (nconc . lists)
[3] (nconc list) => list
For your first code you apply the 3rd rule and
The last list may be any object.
That's:
(nconc 1) => 1
For your second code you apply the 2nd rule then the 3rd one:
(nconc nil 1) == (nconc 1) ;;; is equal to (nconc nil . (1)) == (nconc . (1))
(nconc 1) => 1
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: correct behaviour of nconc

While Goheecha's explanation is correct, an easier-to-understand explanation is that NCONC is the destructive version of APPEND, which is defined as:
CLHS APPEND wrote:... The last argument is not copied; it becomes the CDR of the final dotted pair of the concatenation of the preceding lists, or is returned directly if there are no preceding non-empty lists.
- edgar