Strange behaviour of let() ??

Discussion in 'Cadence' started by Ercan, Sep 2, 2005.

  1. Ercan

    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, Sep 2, 2005
    #1
  2. 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.
    (operator -> is aka putprop) it modifies the list.
    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 Bourguet, Sep 2, 2005
    #2
  3. ....lots of useful stuff snipped...
    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.
     
    Andrew Beckett, Sep 3, 2005
    #3
  4. Ercan

    Ercan Guest

    Thanks for your replies.
     
    Ercan, Sep 5, 2005
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.