newLISP Fan Club

Forum => newLISP newS => Topic started by: Kazimir Majorinc on March 10, 2009, 06:18:33 AM

Title: Local symbols are not deleted outside of their scope.
Post by: Kazimir Majorinc on March 10, 2009, 06:18:33 AM
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?
Title:
Post by: Lutz on March 10, 2009, 07:03:55 AM
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.
Title:
Post by: Kazimir Majorinc on March 10, 2009, 09:55:09 AM
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)))
Title:
Post by: Jeff on March 10, 2009, 10:04:45 AM
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).
Title:
Post by: m35 on March 10, 2009, 03:42:00 PM
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.
Title:
Post by: Kazimir Majorinc on March 11, 2009, 09:59:05 AM
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.