Local symbols are not deleted outside of their scope.

Notices and updates
Locked
Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Local symbols are not deleted outside of their scope.

Post by 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?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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:

Code: Select all

(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
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

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

Code: Select all

(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)))

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

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

Artful code

m35
Posts: 171
Joined: Wed Feb 14, 2007 12:54 pm
Location: Carifornia

Post by m35 »

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
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

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.

Locked