Strange behaviour of let() ??
CADForums.net Forum Index CADForums.net
Discussion of AutoCAD and other CAD software.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web cadforums.net
Strange behaviour of let() ??

 
Post new topic   Reply to topic    CADForums.net Forum Index -> Cadence
Author Message
Ercan
Guest





Posted: Fri Sep 02, 2005 4:10 pm    Post subject: Strange behaviour of let() ?? Reply with quote

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\"")

Back to top
Jean-Marc Bourguet
Guest





Posted: Fri Sep 02, 2005 8:10 pm    Post subject: Re: Strange behaviour of let() ?? Reply with quote

"Ercan" <ercanal@gmail.com> writes:

Quote:
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.

Quote:
in let and in the procedure I'm assigning some values to this list
with operator ->.

(operator -> is aka putprop) it modifies the list.

Quote:
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
Back to top
Andrew Beckett
Guest





Posted: Sat Sep 03, 2005 12:10 pm    Post subject: Re: Strange behaviour of let() ?? Reply with quote

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

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

Quote:
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.

Back to top
Ercan
Guest





Posted: Mon Sep 05, 2005 4:10 pm    Post subject: Re: Strange behaviour of let() ?? Reply with quote

Thanks for your replies.
Back to top
 
Post new topic   Reply to topic    CADForums.net Forum Index -> Cadence All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server DSP VoIP Electronics New Topics
Contact Us
Powered by phpBB