Local symbols are not deleted outside of their scope.

Started by Kazimir Majorinc, March 10, 2009, 06:18:33 AM

Previous topic - Next topic

Kazimir Majorinc

Why local symbols are not deleted outside of their scope? For example, if one writes in REPL



(local (j) j)



and after that



(symbols)



then j is still there. Is it design, implementation or optimization issue?
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

Lutz

#1
It gets part of the symbol-table when the source gets loaded and translated. The Lisp VM only takes care of keeping an environment stack for it. The symbol itself could be used with a different value in another function. Consider the following code:


(let (i 1)
  (let (i 2)
    (let (i 3)
      (print i))
    (print i))
  (println i))


If any of the inner scopes would delete the symbol i, the outer scopes would be in trouble.

Kazimir Majorinc

#2
I do not know is it good, but it gives some supernatural abilities to the symbols.


(set 'self-conscious-symbols
  (lambda()
     (if (not symbols1)
         (set 'symbols1 (symbols)))
     (let ((symbols2
             (difference (symbols)
                 symbols1
                 '(i symbols1 symbols2 self-conscious-symbols))))
                     
       (dolist(i symbols2)
            (set i (append "I am " (string i) ". I feel "
                       (string (apply amb
                                     (difference symbols2
                                           (list i))))
                       " is close."))))))
                     
(self-conscious-symbols)
(seed (date-value))


(begin (self-conscious-symbols)
       (println Sri-Aurobindo)
       
       '(set 'i (list Maharishi
                      Sri-Chinmoy
                      Sai-Baba
                      Dalai-Lama)))
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

Jeff

#3
Once out of scope, they evaluate to nil. In fact, testing against a symbol - even one that is unassigned - creates the symbol, too. Remember the differences between variables, values, and symbols. Once out of scope, the symbol has no value and is not a variable anymore.



Symbols are also created in the context when functions are defined within the context.



It confused me at first too, but it is not generally a problem. The symbols being there adds no overhead (in fact, it reduces it when the symbols are reused).
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

m35

#4
When I was first getting used to newlisp, I wanted it to throw an error if I used a symbol that had never been assigned a value (like Python). I even hacked a bit at the source, but wasn't able to find any easy way. Changing this behavior would probably be a major change to the system, requiring various other parts to change as well.



But this is just one of the many ways (including my wish for a newLint kind of tool) I would like the system to catch me doing dumb things, because I do them a lot.

Kazimir Majorinc

#5
There is the case when it is necessary to take care about deletition of the variables manually. It is - symbols generated during runtime. Modern PCs can keep, say, millions but not billions of Newlisp symbols in the same time.



I described that in my last blog post - Gensym & Genlet.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.