From: lojic
Subject: cdr vs. nthcdr
Date: 
Message-ID: <1172723298.532817.294940@z35g2000cwz.googlegroups.com>
I'm sure I have a fundamental misunderstanding wrt cdr & nthcdr, but
the fact that the following expression is nil bothers me:

(and
  lst
  (eql
    (eql
      (car lst)
      (nth 0 lst))
    (eql
      (cdr lst)
      (nthcdr 0 lst))))

I discovered this while working through "ANSI Common Lisp" and seeing
the following on p. 40:

(maplist #'(lambda (x) x) '(a b c))

which gives

((A B C) (B C) (C))

where I expected

((B C) (C))

since (cdr '(a b c)) gives (B C)

How shall I reconcile this apparent assymetry?

Thanks,
Brian Adkins

From: Vassil Nikolov
Subject: Re: cdr vs. nthcdr
Date: 
Message-ID: <yy8vd53teab4.fsf@eskimo.com>
On 28 Feb 2007 20:28:18 -0800, "lojic" <···········@gmail.com> said:

| I'm sure I have a fundamental misunderstanding wrt cdr & nthcdr, but
| the fact that the following expression is nil bothers me:

| (and
|   lst
|   (eql
|     (eql
|       (car lst)
|       (nth 0 lst))
|     (eql
|       (cdr lst)
|       (nthcdr 0 lst))))

| ...
| How shall I reconcile this apparent assymetry?

  Do not be confused by the fact that "nth" is a prefix of "nthcdr".

  (NTH n list) => the n-th (zero-based) element of list

  (NTHCDR n list) => (CDR ... (CDR list)) with n applications of CDR
                     (where f^0 is identity for any function f)

  ---Vassil.

-- 
mind mate, n.
  One of two persons mentally compatible with each other (cf. soul mate).
From: Vassil Nikolov
Subject: Re: cdr vs. nthcdr
Date: 
Message-ID: <yy8vbqjde9s2.fsf@eskimo.com>
On 28 Feb 2007 22:40:15 -0800, Vassil Nikolov <···············@pobox.com> said:
| ...
|   (NTH n list) => the n-th (zero-based) element of list

|   (NTHCDR n list) => (CDR ... (CDR list)) with n applications of CDR
|                      (where f^0 is identity for any function f)

  Oh, and, of course, (NTH n list) <=> (CAR (NTHCDR n list)) (I think this
  is mentioned in the HyperSpec).  Proof by induction...

  ---Vassil.


-- 
mind mate, n.
  One of two persons mentally compatible with each other (cf. soul mate).
From: Stefan Nobis
Subject: Re: cdr vs. nthcdr
Date: 
Message-ID: <87ps7txx3s.fsf@snobis.de>
"lojic" <···········@gmail.com> writes:

> I'm sure I have a fundamental misunderstanding wrt cdr & nthcdr, but
> the fact that the following expression is nil bothers me:

> (and
>   lst
>   (eql
>     (eql
>       (car lst)
>       (nth 0 lst))
>     (eql
>       (cdr lst)
>       (nthcdr 0 lst))))

Why do you assume that the car and cdr of a cons should be eql?

> I discovered this while working through "ANSI Common Lisp" and seeing
> the following on p. 40:

> (maplist #'(lambda (x) x) '(a b c))

> which gives

> ((A B C) (B C) (C))

> where I expected

> ((B C) (C))

> since (cdr '(a b c)) gives (B C)

CLHS says:

,----
| function is first applied to the lists themselves, and then to the
| cdr of each list, and then to the cdr of the cdr of each list, and
| so on.
`----

-- 
Stefan.
From: Brian Adkins
Subject: Re: cdr vs. nthcdr
Date: 
Message-ID: <sjDFh.188$B7.53@bigfe9>
Stefan Nobis wrote:
> "lojic" <···········@gmail.com> writes:
> 
>> I'm sure I have a fundamental misunderstanding wrt cdr & nthcdr, but
>> the fact that the following expression is nil bothers me:
> 
>> (and
>>   lst
>>   (eql
>>     (eql
>>       (car lst)
>>       (nth 0 lst))
>>     (eql
>>       (cdr lst)
>>       (nthcdr 0 lst))))
> 
> Why do you assume that the car and cdr of a cons should be eql?

I don't assume the car and cdr of a cons should be eql; maybe you should 
look at the expression more closely. However, I did incorrectly assume 
some things about nth and nthcdr that have been cleared up by reading 
the hyperspec - what a great resource!

http://www.lisp.org/HyperSpec/Body/acc_nth.html

http://www.lisp.org/HyperSpec/Body/fun_nthcdr.html

Also, thanks Vassil for relating the two via:

(NTH n list) <=> (CAR (NTHCDR n list))

I'll try and read the hyperspec before posting questions in the future.

>> I discovered this while working through "ANSI Common Lisp" and seeing
>> the following on p. 40:
> 
>> (maplist #'(lambda (x) x) '(a b c))
> 
>> which gives
> 
>> ((A B C) (B C) (C))
> 
>> where I expected
> 
>> ((B C) (C))
> 
>> since (cdr '(a b c)) gives (B C)
> 
> CLHS says:
> 
> ,----
> | function is first applied to the lists themselves, and then to the
> | cdr of each list, and then to the cdr of the cdr of each list, and
> | so on.
> `----
> 
From: Stefan Nobis
Subject: Re: cdr vs. nthcdr
Date: 
Message-ID: <87irdkgfcu.fsf@snobis.de>
Brian Adkins <·················@gmail.com> writes:

> I don't assume the car and cdr of a cons should be eql; maybe you
> should look at the expression more closely.

Yes, you are right. Thanks for the correction.

-- 
Stefan.