[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

difference between cdr and nthcdr

james

11/15/2015 6:43:00 AM

(setf a '(1 2 3))
CL-USER> (eq (cdr a) (nthcdr 1 a))
T
Try to insert a new element with nthcdr failed,but with cdr there is not any problem. Any reason?

CL-USER> (setf (nthcdr 1 a ) (cons 2 (nthcdr 1 a)))

; in: SETF (NTHCDR 1 A)
; (FUNCALL #'(SETF NTHCDR) #:NEW936 1 #:A935)
; ==>
; (SB-C::%FUNCALL #'(SETF NTHCDR) #:NEW936 1 #:A935)
;
; caught WARNING:
; The function (SETF NTHCDR) is undefined, and its name is reserved by ANSI CL so
; that even if it were defined later, the code doing so would not be portable.

; (NTHCDR 1 A)
;
; caught WARNING:
; undefined variable: A
;
; compilation unit finished
; Undefined function:
; (SETF NTHCDR)
; Undefined variable:
; A
; caught 2 WARNING conditions

CL-USER> (setf (cdr a) (cons 5 (cdr a)))
(5 2 3)
CL-USER> a
(1 5 2 3)
16 Answers

Teemu Likonen

11/15/2015 6:49:00 AM

0

james [2015-11-14 22:43:10-08] wrote:

> Try to insert a new element with nthcdr failed,but with cdr there is
> not any problem. Any reason?

NTHCDR is a function (not an accessor) so it can't be used with SETF.

Teemu Likonen

11/15/2015 7:16:00 AM

0

Teemu Likonen [2015-11-15 08:48:50+02] wrote:

> NTHCDR is a function (not an accessor) so it can't be used with SETF.

But since this is CL we can create such accessor:

(defun (setf nthcdr*) (new-tail n list)
(loop :for i :upfrom 0
:for current := (cons nil list) :then (cdr current)
:while current
:if (= i n) :do (return (setf (cdr current) new-tail))))

CL-USER> (let ((list (list 1 2 3 4 5)))
(setf (nthcdr* 2 list) (list 'a 'b 'c))
list)
(1 2 A B C)

William James

11/15/2015 8:01:00 AM

0

Teemu Likonen wrote:

> Teemu Likonen [2015-11-15 08:48:50+02] wrote:
>
> > NTHCDR is a function (not an accessor) so it can't be used with SETF.
>
> But since this is CL we can create such accessor:
>
> (defun (setf nthcdr*) (new-tail n list)
> (loop :for i :upfrom 0
> :for current := (cons nil list) :then (cdr current)
> :while current
> :if (= i n) :do (return (setf (cdr current) new-tail))))
>
> CL-USER> (let ((list (list 1 2 3 4 5)))
> (setf (nthcdr* 2 list) (list 'a 'b 'c))
> list)
> (1 2 A B C)

MatzLisp (Ruby):

a = [1,2,3,4,5]
==>[1, 2, 3, 4, 5]
a[2 .. -1] = %w(a b c)
==>["a", "b", "c"]
a
==>[1, 2, "a", "b", "c"]
a = [1,2,3,4,5]
==>[1, 2, 3, 4, 5]
a[2 .. -1] = %w(a b c d)
==>["a", "b", "c", "d"]
a
==>[1, 2, "a", "b", "c", "d"]

--
Africans gang-rape and clitorectomize Finnish girl; government arrests Finn
whom they accuse of complaining:
conservative-headlines.com/2009/03/another-european-awaits-extradition-for-hate-speech

Teemu Likonen

11/15/2015 9:20:00 AM

0

Teemu Likonen [2015-11-15 09:15:56+02] wrote:

> But since this is CL we can create such accessor:
>
> (defun (setf nthcdr*) (new-tail n list)
> (loop :for i :upfrom 0
> :for current := (cons nil list) :then (cdr current)
> :while current
> :if (= i n) :do (return (setf (cdr current) new-tail))))

Hmm. Maybe (setf (nthcdr* 0 ...) ...) should modify the first cons of
the LIST, so:

(defun (setf nthcdr*) (new-tail n list)
(if (= n 0)
(setf (car list) (car new-tail)
(cdr list) (cdr new-tail))
(loop :for i :upfrom 1
:for current := list :then (cdr current)
:while current
:if (= i n) :do (setf (cdr current) new-tail)))
new-tail)

Teemu Likonen

11/15/2015 9:25:00 AM

0

Teemu Likonen [2015-11-15 09:15:56+02] wrote:

> But since this is CL we can create such accessor:
>
> (defun (setf nthcdr*) (new-tail n list)
> (loop :for i :upfrom 0
> :for current := (cons nil list) :then (cdr current)
> :while current
> :if (= i n) :do (return (setf (cdr current) new-tail))))

Hmm. Maybe (setf (nthcdr* 0 ...) ...) should modify the first cons of
the LIST, so:


(defun (setf nthcdr*) (new-tail n list)
;; NTHCDR accessor
(if (= n 0)
(progn (setf (car list) (car new-tail)
(cdr list) (cdr new-tail))
new-tail)
(loop :for i :upfrom 1
:for current := list :then (cdr current)
:while current
:if (= i n) :do (return (setf (cdr current) new-tail)))))

james

11/15/2015 1:21:00 PM

0

On Sunday, November 15, 2015 at 5:24:43 PM UTC+8, Teemu Likonen wrote:
> Teemu Likonen [2015-11-15 09:15:56+02] wrote:
>
> > But since this is CL we can create such accessor:
> >
> > (defun (setf nthcdr*) (new-tail n list)
> > (loop :for i :upfrom 0
> > :for current := (cons nil list) :then (cdr current)
> > :while current
> > :if (= i n) :do (return (setf (cdr current) new-tail))))
>
> Hmm. Maybe (setf (nthcdr* 0 ...) ...) should modify the first cons of
> the LIST, so:
>
>
> (defun (setf nthcdr*) (new-tail n list)
> ;; NTHCDR accessor
> (if (= n 0)
> (progn (setf (car list) (car new-tail)
> (cdr list) (cdr new-tail))
> new-tail)
> (loop :for i :upfrom 1
> :for current := list :then (cdr current)
> :while current
> :if (= i n) :do (return (setf (cdr current) new-tail)))))

Thanks, very helpful.

Pascal Costanza

11/15/2015 4:10:00 PM

0

On 15/11/2015 08:15, Teemu Likonen wrote:
> Teemu Likonen [2015-11-15 08:48:50+02] wrote:
>
>> NTHCDR is a function (not an accessor) so it can't be used with SETF.
>
> But since this is CL we can create such accessor:
>
> (defun (setf nthcdr*) (new-tail n list)
> (loop :for i :upfrom 0
> :for current := (cons nil list) :then (cdr current)
> :while current
> :if (= i n) :do (return (setf (cdr current) new-tail))))
>
> CL-USER> (let ((list (list 1 2 3 4 5)))
> (setf (nthcdr* 2 list) (list 'a 'b 'c))
> list)
> (1 2 A B C)
>

Not a good idea: See bullet 14 at
http://www.lispworks.com/documentation/HyperSpec/Body/1...

Pascal

--
My website: http:/...
Common Lisp Document Repository: http://cdr.eu...
Closer to MOP & ContextL: http://common-lisp.net/proje...
The views expressed are my own, and not those of my employer.

Kaz Kylheku

11/15/2015 4:18:00 PM

0

On 2015-11-15, james <dinglei2008@gmail.com> wrote:
> (setf a '(1 2 3))
> CL-USER> (eq (cdr a) (nthcdr 1 a))
> T

There is no difference between (cdr x) and (nthcdr 1 x) as functions.

However cdr is an accessor, whereas nthcdr is just a function.

This means you can do (setf (cdr x) y), but not (setf (nthcdr 1 x) y).

(Except as an extension provided by your Lisp.)

Teemu Likonen

11/15/2015 4:19:00 PM

0

Pascal Costanza [2015-11-15 17:10:17+01] wrote:

> On 15/11/2015 08:15, Teemu Likonen wrote:
>> (defun (setf nthcdr*) (new-tail n list)

> Not a good idea: See bullet 14 at
> http://www.lispworks.com/documentation/HyperSpec/Body/1...

I named it NTHCDR*.

Pascal Costanza

11/15/2015 4:30:00 PM

0

On 15/11/2015 17:18, Teemu Likonen wrote:
> Pascal Costanza [2015-11-15 17:10:17+01] wrote:
>
>> On 15/11/2015 08:15, Teemu Likonen wrote:
>>> (defun (setf nthcdr*) (new-tail n list)
>
>> Not a good idea: See bullet 14 at
>> http://www.lispworks.com/documentation/HyperSpec/Body/1...
>
> I named it NTHCDR*.
>

Oh right. Sorry for the noise... ;)

Pascal

--
My website: http:/...
Common Lisp Document Repository: http://cdr.eu...
Closer to MOP & ContextL: http://common-lisp.net/proje...
The views expressed are my own, and not those of my employer.