Strange behaviour of let() ??

E

Ercan

Guest
Hi,

I'm using a local variable and initializing this variable to an empty
list in let and in the procedure I'm assigning some values to this list
with operator ->. But If I run same function again. This time the list
is not empty. Isn't this strange?

I tried this with the following function.

procedure(temp(A b)
let( ( (mode '(nil)) )
println(mode)
when(b
mode->a = sprintf(nil "%L" A)
)
println(mode)
)
)


The output in CDS.LOG file is
\i load "~/test.il"
\t t
\p >
\i temp("SDv" t)
\o (nil)
\o (nil a "\"SDv\"")
\t nil
\p >
\i temp("SDv" t)
\o (nil a "\"SDv\"")
\o (nil a "\"SDv\"")
 
"Ercan" <ercanal@gmail.com> writes:

Hi,

I'm using a local variable and initializing this variable to an empty
list
No, you are initializing it to a list containing the only value nil
(an empty list). A list containing an empty list is not empty.

in let and in the procedure I'm assigning some values to this list
with operator ->.
(operator -> is aka putprop) it modifies the list.

But If I run same function again. This time the list is not
empty.
Short answer: not really, you have modified the list used to
initialise your variable. Copy the list with append and you'll get
the behaviour you want.

Long answer:

Check out
(defun ercan ()
(let (result '(nil))
result))
(setq a (ercan))
(setq b (ercan))
(setq c '(nil))
(eq a b)
(eq a c)

The first test show that it is the same list which is returned for the
two calls. The second test show that eq do object equality and not
structural equality (use equal to do structural equality test).

This show that in a let, it is the same object (if you use quote) that
initialise the variables.

Now try

(putprop a '1 'foo)
a
b
(ercan)

Now you see that putprop modify the list and so all mean to access
it. If you want to get a copy a of list, the easiest way is probably
to append nil to it. So using
(let ((mode (append '(nil) nil)))
...
should do what you want.

Yours,

--
Jean-Marc
 
On 02 Sep 2005 18:20:47 +0200, Jean-Marc Bourguet <jm@bourguet.org> wrote:

....lots of useful stuff snipped...

Now you see that putprop modify the list and so all mean to access
it. If you want to get a copy a of list, the easiest way is probably
to append nil to it. So using
(let ((mode (append '(nil) nil)))
...
should do what you want.

Yours,
I cover this in some detail in my sourcelink solution:

Number: 11024308
Title: Why does my SKILL function remember previous local disembodied property
lists?

A better solution than using append to build the initial list is to just do:

(let ((mode (ncons nil)) ...

or use (list nil) instead of ncons. There's no point appending nil to a quoted
list.

Effectively, quote returns a pointer to the literal list, and then -> is a
destructive list operator which modifies that literal list in place (doing a pp
of the code will show you it has changed). list() or ncons() will build a fresh
list each time it's called, and so -> will still destructively modify it, but at
least it will be a new list each time.

Regards,

Andrew.
 

Welcome to EDABoard.com

Sponsor

Back
Top