From: Oudeis
Subject: push and rplaca
Date: 
Message-ID: <9cca45d2.0308161627.3f01140c@posting.google.com>
I know this is a show of profound ignorance on my part,
so if you feel too embarassed to answer just point me 
to someplace in Hyperspec or CLTL.

(defun ppush (item place)
    (push item place))


(defun rrplaca (cons object)
    (rplaca cons object))



(let ((list (list 4)))
    (ppush 2 list)
    list)
=>(4)

(let ((list (list 4)))
    (rrplaca list 2)
    list)
=>(2)

Why did list remain unchanged after a call to ppush
and was modified after a call to rrplaca?

From: Peter Seibel
Subject: Re: push and rplaca
Date: 
Message-ID: <m33cg1p0wa.fsf@javamonkey.com>
····@hotmail.com (Oudeis) writes:

> I know this is a show of profound ignorance on my part, so if you
> feel too embarassed to answer just point me to someplace in
> Hyperspec or CLTL.

You can read about PUSH in the HyperSpec as part of the Conses
Dictionary (Section 14.2). If you do, you'll see that 

  (push item place)

is equivalent to

  (setf place (cons item place))

That is, PUSH operates on a *variable*, not an *object*. If that
distiction (variable vs object) is not immediately clear to you, you
may want to ponder it for a while and see if you become enlightened.
Reading Section 5.1 Generalized Reference may also help.

> (defun ppush (item place)
>     (push item place))

This function is equivalent, based on the definition of PUSH, to:

  (defun ppush (item place)
    (setf item (cons item place)))

In other words, the variable PLACE initially refers to whatever object
is passed as the second argument to PPUSH. Then the variable is set to
refer to a new cons cell made up of the object passed as ITEM and the
object PLACE originally refers to. Which of course has no effect on
that object.

> (defun rrplaca (cons object)
>     (rplaca cons object))

In this case, RPLACA actually modifies the *object* refered to by
cons, changing its CAR. Slightly more modern would be to use SETF of
the CAR rather than RPLACA:

  (defun rrplaca (cons object)
    (setf (car cons) object))

-Peter

> (let ((list (list 4)))
>     (ppush 2 list)
>     list)
> =>(4)
> 
> (let ((list (list 4)))
>     (rrplaca list 2)
>     list)
> =>(2)
> 
> Why did list remain unchanged after a call to ppush
> and was modified after a call to rrplaca?

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Kenny Tilton
Subject: Re: push and rplaca
Date: 
Message-ID: <3F3EE196.8010701@nyc.rr.com>
Oudeis wrote:
> I know this is a show of profound ignorance on my part,
> so if you feel too embarassed to answer just point me 
> to someplace in Hyperspec or CLTL.
> 
> (defun ppush (item place)
>     (push item place))
> 
> 
> (defun rrplaca (cons object)
>     (rplaca cons object))
> 
> 
> 
> (let ((list (list 4)))
>     (ppush 2 list)
>     list)
> =>(4)
> 
> (let ((list (list 4)))
>     (rrplaca list 2)
>     list)
> =>(2)
> 
> Why did list remain unchanged after a call to ppush
> and was modified after a call to rrplaca?

push created a new list which ppush ignored, returning the original 
list. push made a new cons cell and pointed its cdr at the input list. 
it returned that new cons cell, which ppush ignores. son to be gc'ed.

rplaca destructively modifies the cons cell pointed to by the input 
list. you do return that, and then you do see the new value.

drop back from all this and learn one very Deep lesson. There are no 
such things as lists. there are only cons cells. you must learn to think 
in terms of cons cells.

btw, where are the clowns that did not buy my argument along these lines 
a few months ago. :) i was dissing first and rest because they masked 
the cons reality, if that helps you remember.

<g>

-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Career highlights? I had two. I got an intentional walk from
Sandy Koufax and I got out of a rundown against the Mets."
                                                  -- Bob Uecker
From: Kaz Kylheku
Subject: Re: push and rplaca
Date: 
Message-ID: <cf333042.0308181002.4c64abcc@posting.google.com>
····@hotmail.com (Oudeis) wrote in message news:<····························@posting.google.com>...
> I know this is a show of profound ignorance on my part,
> so if you feel too embarassed to answer just point me 
> to someplace in Hyperspec or CLTL.
> 
> (defun ppush (item place)
>     (push item place))

Please read the recent thread whose subject line is ``totally
baffled''. It's about a similar problem: why INCF written as a
function doesn't work ...
 
> (defun rrplaca (cons object)
>     (rplaca cons object))

... and why in some cases functions *can* modify an object that is
passed in.
 

> Why did list remain unchanged after a call to ppush
> and was modified after a call to rrplaca?

The short answer is that PPUSH updates the value binding of the local
variable called PLACE, but RRPLACA works on the car of the cons cell
object that was passed in.

The PUSH operator does not manipulate the structure of a list object;
it manipulates the storage location which refers to the first cell of
a list. In your case, that storage location is a lexical variable,
local to the PPUSH function.