From: Mark
Subject: memory problems
Date: 
Message-ID: <6csdjo$q6g@catapult.gatech.edu>
Okay I don't know much about memory and variables so give
me a break if this is really stupid question.

I have a data structure that is declared as follows
(defconstant '(blah blah blah...))

I have some functions that extract information from it
using assoc, I then pass the results of this to some
other functions that alter it, the problem is it alters
the original data structure as well.

I want to alter a local copy of the information and
not the original how can I do this.



--
mec --gt2863a
 

From: Sunil Mishra
Subject: Re: memory problems
Date: 
Message-ID: <efyu39qp3ed.fsf@peachtree.cc.gatech.edu>
In article <··········@catapult.gatech.edu> ·······@acmey.gatech.edu (Mark) writes:

   Okay I don't know much about memory and variables so give
   me a break if this is really stupid question.

   I have a data structure that is declared as follows
   (defconstant '(blah blah blah...))

   I have some functions that extract information from it
   using assoc, I then pass the results of this to some
   other functions that alter it, the problem is it alters
   the original data structure as well.

   I want to alter a local copy of the information and
   not the original how can I do this.

Have you ever learned the box-and-pointer notation for representing cons
cells? Probably not, otherwise this would not be an issue.

Let me try and explain it in C terms. I am going to ignore typing issues,
since Lisp does too. Think of a cons cell as a two element array. Each time
you cons, you create a new array. A list is then just a set of these arrays
attached to one another.

So, when you assign to a variable a cons cell as value, the variable gets a
pointer to the cons cell. This pointer in Lisp (and Java) is invisible, but
every implementation has ways of getting to it. When you refer to the value
of the variable, you refer to the value that the pointer holds. When you
use the defconstant form, it simply tells Lisp that the pointer held by the
variable ought not be changed. In effect, you can't change the value of the
variable itself. It will forever remain eq to all of its other values.

However, you can modify anything downstream from the variable. Although the
pointer remains eq, the contents downstream change. Hence the reason for
avoiding side effect producing forms, like setf.

If you want a way out of this situation, don't use setf, delete, or any
other destructive function on the constant you have defined, or any parts
of it. If you *need* to use destructive operations, make a copy
first. CLtL2 and the hyperspec (at www.harlequin.com) explicitly state
which functions you can expect to be destructive. Given the sparse
description of your problem, this is the best response I can give.

Sunil
From: Barry Margolin
Subject: Re: memory problems
Date: 
Message-ID: <BGkI.10$us2.35352@cam-news-reader1.bbnplanet.com>
In article <··········@catapult.gatech.edu>,
Mark <·······@acmey.gatech.edu> wrote:
>I want to alter a local copy of the information and
>not the original how can I do this.

Functions like COPY-TREE and COPY-LIST may be helpful.  Use them when
passing the data structures to functions that can alter their parameters.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.
From: Huy V. Le
Subject: COPY-LIST ?
Date: 
Message-ID: <01bd409c$434c6d80$98f360cf@le>
> Functions like COPY-TREE and COPY-LIST may be helpful.  Use them when

Are they standard function or you have to build them?

Sincerely,

Huy
From: Huy V. Le
Subject: Re: COPY-LIST ?
Date: 
Message-ID: <01bd409d$0a69f040$98f360cf@le>
Where did I have my head, here an example that may help someone

(defun swap (i j aList)
  (let ((list (copy-list aList)))
    (rotatef (nth i list) (nth j list))
    list
    )
  )

> Are they standard function or you have to build them?

 Sincerely,

 Huy
From: Barry Margolin
Subject: Re: COPY-LIST ?
Date: 
Message-ID: <bGpI.17$us2.263774@cam-news-reader1.bbnplanet.com>
In article <··························@le>,
Huy V. Le <······@horslimites.qc.ca> wrote:
>> Functions like COPY-TREE and COPY-LIST may be helpful.  Use them when
>
>Are they standard function or you have to build them?

They're standard functions.  Look them up in CLtL or the HyperSpec.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.