From: Luke
Subject: Clear Lists from memory
Date: 
Message-ID: <bfdg7k$u44$1@online.de>
Hello!

My question is very simple.

How can I delete List-Structures from memory in Lisp?

I tried

(setq var nil)

thinking that the garbage collection would do the rest, but var contains the
same as befor after this... (???)


Luke

From: Alex Drummond
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfdl9o$3lq$1@news8.svr.pol.co.uk>
You don't have to set the list to nil for the garbage collector to collect
it. I'm not quite sure what your question means, could you post some more
code so we can see the context which the setq appears in?

Alex


Luke wrote:

> Hello!
> 
> My question is very simple.
> 
> How can I delete List-Structures from memory in Lisp?
> 
> I tried
> 
> (setq var nil)
> 
> thinking that the garbage collection would do the rest, but var contains
> the same as befor after this... (???)
> 
> 
> Luke
From: Coby Beck
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfdj0l$21jm$1@otis.netspace.net.au>
"Luke" <···················@gmx.de> wrote in message
·················@online.de...
> Hello!
>
> My question is very simple.
>
> How can I delete List-Structures from memory in Lisp?
>
> I tried
>
> (setq var nil)
>
> thinking that the garbage collection would do the rest, but var contains
the
> same as befor after this... (???)

Why do you think that?  What implementation are you using?

CL-USER 1 > (defvar *foo* (list 'a 'long 'list 'goes 'here))
*FOO*

CL-USER 2 > *foo*
(A LONG LIST GOES HERE)

CL-USER 3 > (setf *foo* nil)
NIL

CL-USER 4 > *foo*
NIL

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")
From: Luke
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfdlul$38h$1@online.de>
"Coby Beck" <·····@mercury.bc.ca> wrote in message
··················@otis.netspace.net.au...
>
>
> Why do you think that?  What implementation are you using?

CLISP.

> CL-USER 1 > (defvar *foo* (list 'a 'long 'list 'goes 'here))
> *FOO*
>
> CL-USER 2 > *foo*
> (A LONG LIST GOES HERE)
>
> CL-USER 3 > (setf *foo* nil)
> NIL
>
> CL-USER 4 > *foo*
> NIL


(defvar CLAUSEINST '())
(defvar NEWREPLS '(()))
(defvar NEWREPLS2 '())
(defvar NEWGL '())
(defvar uservars '(()))

.....

(defun clear-memory! ()
  (progn
     ....
    (setq CLAUSEINST '())
    (setqNEWREPLS '(()))
    (setq NEWREPLS2 '())
    (setq NEWGL '())
    (setq uservars '(()))
    )
)

So this is the procedure I use. I have tested it right now once more and
found something interesting: all variables are set to the right values
except the last (uservars).
Why??

And doing so, is it sure that the memory-area of those variables will get
cleared?

Luke
From: Rainer Joswig
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <joswig-999A60.12071120072003@news.fu-berlin.de>
In article <············@online.de>,
 "Luke" <···················@gmx.de> wrote:

> "Coby Beck" <·····@mercury.bc.ca> wrote in message
> ··················@otis.netspace.net.au...
> >
> >
> > Why do you think that?  What implementation are you using?
> 
> CLISP.
> 
> > CL-USER 1 > (defvar *foo* (list 'a 'long 'list 'goes 'here))
> > *FOO*
> >
> > CL-USER 2 > *foo*
> > (A LONG LIST GOES HERE)
> >
> > CL-USER 3 > (setf *foo* nil)
> > NIL
> >
> > CL-USER 4 > *foo*
> > NIL
> 
> 
> (defvar CLAUSEINST '())
> (defvar NEWREPLS '(()))
> (defvar NEWREPLS2 '())
> (defvar NEWGL '())
> (defvar uservars '(()))
> 
> .....
> 
> (defun clear-memory! ()
>   (progn
>      ....
>     (setq CLAUSEINST '())
>     (setqNEWREPLS '(()))
>     (setq NEWREPLS2 '())
>     (setq NEWGL '())
>     (setq uservars '(()))
>     )
> )
> 
> So this is the procedure I use. I have tested it right now once more and
> found something interesting: all variables are set to the right values
> except the last (uservars).
> Why??

Because you are doing something wrong.

> And doing so, is it sure that the memory-area of those variables will get
> cleared?

The variables are basically just pointers to data. Thinking
about clearing memory-areas is not really necessary in Lisp.

Try something like this:

(defun clear-memory! ()

 ...

  (setq uservars (list ())
...
)

My guess is that you have a constant list '(())  in your
procedure and that some other code is changing that constant.
So you assign later always the changed datastructure
when calling CLEAR-MEMORY.
If you want to set it to a NEW list with one empty list
as its only element you have to use (LIST ()) .
Which is the same as (LIST '()) or (LIST NIL) .
From: Luke
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfds1f$96n$1@online.de>
> My guess is that you have a constant list '(())  in your
> procedure and that some other code is changing that constant.
> So you assign later always the changed datastructure
> when calling CLEAR-MEMORY.
> If you want to set it to a NEW list with one empty list
> as its only element you have to use (LIST ()) .
> Which is the same as (LIST '()) or (LIST NIL) .

Thankyou very much!

I'm not completely understanding why, but your guess seems to be right!

Now it works very fine!

Luke
From: Rainer Joswig
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <joswig-21FCA7.13165720072003@news.fu-berlin.de>
In article <············@online.de>,
 "Luke" <···················@gmx.de> wrote:

> > My guess is that you have a constant list '(())  in your
> > procedure and that some other code is changing that constant.
> > So you assign later always the changed datastructure
> > when calling CLEAR-MEMORY.
> > If you want to set it to a NEW list with one empty list
> > as its only element you have to use (LIST ()) .
> > Which is the same as (LIST '()) or (LIST NIL) .
> 
> Thankyou very much!
> 
> I'm not completely understanding why, but your guess seems to be right!
> 
> Now it works very fine!
> 
> Luke
> 
> 

CL-USER 15 > (defun foo ()
               '(a b c))            ; the list is a literal constant in 
the code!
FOO

CL-USER 16 > (defvar *bar* nil)     ; declaring a top-level variable
*BAR*

CL-USER 17 > (setf *bar* (foo))     ; setting the variable
(A B C)

CL-USER 18 > *bar*                  ; looks fine
(A B C)

CL-USER 19 > (setf (nth 1 *bar*) 'changed) ; Problem: changing an 
element in this list is not really allowed according to ANSI CL.
CHANGED

CL-USER 20 > *bar*                  ; oops, literal constant is changed
(A CHANGED C)

CL-USER 21 > (setf *bar* (foo))     ; oops, also the list inside 
function is affected
(A CHANGED C)

CL-USER 22 > *bar*
(A CHANGED C)

-------

Above is one possible way what could happen. Since
it is not really defined what happens when you
modify a literal constant, other szenarios (heap corruption, ...)
are possible.
From: Kenny Tilton
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <3F1A9988.20702@nyc.rr.com>
Luke wrote:
>>My guess is that you have a constant list '(())  in your
>>procedure and that some other code is changing that constant.
>>So you assign later always the changed datastructure
>>when calling CLEAR-MEMORY.
>>If you want to set it to a NEW list with one empty list
>>as its only element you have to use (LIST ()) .
>>Which is the same as (LIST '()) or (LIST NIL) .
> 
> 
> Thankyou very much!
> 
> I'm not completely understanding why, ...

It's not your fault. Intros to Lisp tend to have lots of early examples 
that use lists like '(dogs cats guppies). Because that is a lot nicer 
than (list 'dogs 'cats 'guppies).

So newbies think that's how we make lists. But those are like string 
literals in C: the compiler can stash them somewhere and reuse them at 
will. So you cannot operate on them destructively without getting 
unexpected changes wherever the same literal was used.



-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Everything is a cell." -- Alan Kay
From: Luke
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfeuo0$agh$1@online.de>
Thanks, Rainer and Kenny!

I think I understand (partly).

I'll have to read a little bit more about lists.
But I'm happy now, that it works. It made me crazy, cause the things that
happened seemed quite unlogical to me...

greetings
From: Will Hartung
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfjq5i$ffb6n$1@ID-197644.news.uni-berlin.de>
"Luke" <···················@gmx.de> wrote in message
·················@online.de...
> Thanks, Rainer and Kenny!
>
> I think I understand (partly).
>
> I'll have to read a little bit more about lists.
> But I'm happy now, that it works. It made me crazy, cause the things that
> happened seemed quite unlogical to me...

You need to get your head around it, because it is in fact really quite
logical (it's a computer after all), however as Kenny said, it's not
necessarily you as much as the books examples.

When you have code like this:

(setf VAR '(a static list))

Then VAR will point to the head of a list that was created when the file was
compiled. Now remember that a list is not a monolithic structure (like, say,
an array is). Rather, it's collection of CONS cells that point to each
other.

VAR is pointing to only the first of those CONS cells (in this case, the CAR
of the CONS cell points to the symbol A, whereas the CDR of the CONS cell
points to another CONS cell (the one with a CAR pointing to the symbol
STATIC).

So, if you change any of the CONSes besides the first one, then those
changes will affect VAR.

Don't fret...everyone eventually gets bit by this.

Regards,

Will Hartung
(·····@msoft.com)
From: Coby Beck
Subject: Re: Clear Lists from memory
Date: 
Message-ID: <bfficb$1a12$1@otis.netspace.net.au>
"Luke" <···················@gmx.de> wrote in message
·················@online.de...
>
> (defun clear-memory! ()
>   (progn
>      ....
>     (setq CLAUSEINST '())
>     (setqNEWREPLS '(()))
>     (setq NEWREPLS2 '())
>     (setq NEWGL '())
>     (setq uservars '(()))
>     )
> )
>
> So this is the procedure I use. I have tested it right now once more and
> found something interesting: all variables are set to the right values
> except the last (uservars).
> Why??

Others have told you the cause of your specific problem, using quoted list
structure, but at its root is something more basic: let go of you desire to
do things like
(defun clear-memory! ....)

Memory management is supposed to be transparent to the lisp programmer.
Sometimes for very special reasons people do take a little more control but
it is not the norm and I would wager you don't really need to think about it
for your (admittedly unknown) purposes.

Close your eyes, and let yourself fall...!

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")