From: 92S10711
Subject: why is my structure being changed by function
Date: 
Message-ID: <28MAR199215282934@rosie.uh.edu>
i am passing a structure and an array to a function (along with a couple of 
number parameters).  inside the function i do some setfs to the array and some
setfs to the structure.  the function returns the new version of the structure
which is cons-ed onto a list being made by the calling function.  i am 
trying to make several versions of the original structure instance so the
calling function is repeatedly calling the make-new-version function with 
slightly different parameters each time.  

my problem is that when the calling function sends to the make-new-version 
function the second and subsequent times, the calling
function sends the changed structure and array made by most recent call, 
instead of the original structure and array. the calling function 
itself performs no setfs on the structure and array and the changed array is 
not (or so i thought) returned to the calling function.  i thought (from 
Winston, 3d) that actions inside
a function did not permanently affect the variables named in the parameter list.

i tried declaring some variables in a let inside the new-version-function and 
initializing them to the structure and array and changing the let-declared 
variables instead, but the original structure and 
array of the calling function were still changed.  it seems like the setf 
is reaching outside of the function it is in.  

i also have a copy of steele CLtL, 2d but am not yet familiar enough with it to know
where to get an explanation of what is going on.  i would greatly appreciate
some references to read in either of the above books and/or a short explanation
of why the structure and array appear to be acting like special variables.

D L Hall

From: Milt Epstein
Subject: Re: why is my structure being changed by function
Date: 
Message-ID: <1992Mar29.043952.23265@sunb10.cs.uiuc.edu>
In <·················@rosie.uh.edu> ········@rosie.uh.edu (92S10711) writes:

>i am passing a structure and an array to a function (along with a
>couple of number parameters).  inside the function i do some setfs to
>the array and some setfs to the structure.  the function returns the
>new version of the structure which is cons-ed onto a list being made
>by the calling function.  i am trying to make several versions of the
>original structure instance so the calling function is repeatedly
>calling the make-new-version function with slightly different
>parameters each time.
>
>my problem is that when the calling function sends to the
>make-new-version function the second and subsequent times, the calling
>function sends the changed structure and array made by most recent
>call, instead of the original structure and array. the calling
>function itself performs no setfs on the structure and array and the
>changed array is not (or so i thought) returned to the calling
>function.  i thought (from Winston, 3d) that actions inside a function
>did not permanently affect the variables named in the parameter list.
>
>i tried declaring some variables in a let inside the
>new-version-function and initializing them to the structure and array
>and changing the let-declared variables instead, but the original
>structure and array of the calling function were still changed.  it
>seems like the setf is reaching outside of the function it is in.
>
>i also have a copy of steele CLtL, 2d but am not yet familiar enough
>with it to know where to get an explanation of what is going on.  i
>would greatly appreciate some references to read in either of the
>above books and/or a short explanation of why the structure and array
>appear to be acting like special variables.

It sounds like you are going to need to make copies of your arrays and
structures before you make new versions of them.  I don't know what
function(s) you can use to make copies of arrays, but when you create
a structure, a copy function is automatically created, and it is
called copy-<name>, where <name> is the structure name.

As to actions in a function permanently affecting the variables named
in the parameter list, it depends on what types of actions those are.
Basically, non-destructive operations will not change them, but
destructive operations will.  The variable in the parameter list
becomes a pointer to the value of the argument passed in.  If you do a
setf on that variable, you are only changing what object it is
pointing to.  If you do a setf on some contents of that variable (or
some other destructive operation), you are actually changing the
object it is pointing to, which will affect the value of the argument
passed in.

For example, consider the following function and expressions:

     (defun foo (x y)
       (setf y 5)
       (setf (car x) '*))

     (setf a '(+ 3 5))

     (setf b 6)

     (foo a b)

After this function call, b is unchanged, but a is now (* 3 5).

Since structures and arrays are set similar to the way the variable x
was changed in function foo, you are getting the behavior you observed
above.

I looked thropugh CLTL, but I could not find where this topic was
covered.  I'm sure others will mention this and perhaps give a more
comprehensive explanation of what is going on.

Hope this helps.

-- 
Milt Epstein
Department of Computer Science
University of Illinois
·······@cs.uiuc.edu
From: 92S10711
Subject: Re: why is my structure being changed by function
Date: 
Message-ID: <29MAR199200081373@rosie.uh.edu>
In article <······················@sunb10.cs.uiuc.edu>, ·······@cs.uiuc.edu writes...
>In <·················@rosie.uh.edu> ········@rosie.uh.edu (92S10711) writes:
> 
> 
>I looked thropugh CLTL, but I could not find where this topic was
>covered.  I'm sure others will mention this and perhaps give a more
>comprehensive explanation of what is going on.
> 
>Hope this helps.
> 
>-- 
>Milt Epstein

Thanks for the explanation.  Your example pinpointed my problem exactly.
Thanks for the suggestion about copying.  I tried copying in the calling 
function but NOT inside the called function.
From: Scott McKay
Subject: why is my structure being changed by function
Date: 
Message-ID: <19920330141509.6.SWM@SUMMER.SCRC.Symbolics.COM>
    Date: Sat, 28 Mar 1992 16:28 EST
    From: 92S10711 <········@rosie.uh.edu>

    i am passing a structure and an array to a function (along with a couple of 
    number parameters).  inside the function i do some setfs to the array and some
    setfs to the structure.  the function returns the new version of the structure
    which is cons-ed onto a list being made by the calling function.  i am 
    trying to make several versions of the original structure instance so the
    calling function is repeatedly calling the make-new-version function with 
    slightly different parameters each time.  

    my problem is that when the calling function sends to the make-new-version 
    function the second and subsequent times, the calling
    function sends the changed structure and array made by most recent call, 
    instead of the original structure and array. the calling function 
    itself performs no setfs on the structure and array and the changed array is 
    not (or so i thought) returned to the calling function.  i thought (from 
    Winston, 3d) that actions inside
    a function did not permanently affect the variables named in the parameter list.

Lisp variables are object references, not objects.  You can think of them
as pointers to objects.  So SETFing a slot in a structure or an element
of the array does not change the object reference, but side-effects the
object.  This is no different than passing a pointer to a procedure in C,
and then modifying something that the pointer points to.

    i tried declaring some variables in a let inside the new-version-function and 
    initializing them to the structure and array and changing the let-declared 
    variables instead, but the original structure and 
    array of the calling function were still changed.  it seems like the setf 
    is reaching outside of the function it is in.  

LET does not copy, it creates a new object reference.

    i also have a copy of steele CLtL, 2d but am not yet familiar enough with it to know
    where to get an explanation of what is going on.  i would greatly appreciate
    some references to read in either of the above books and/or a short explanation
    of why the structure and array appear to be acting like special variables.

    D L Hall