dynamic binding in newLISP

Started by dido, August 11, 2006, 09:59:54 AM

Previous topic - Next topic

dido

I've been exploring newLISP lately and one of the things I've noticed is that unlike most modern LISP dialects such as Scheme and ANSI Common LISP, newLISP uses dynamic binding to obtain the values of free variables. The following code:



(let ((x 1))
  (let ((f (lambda (y)
   (+ x y)))
(g (lambda (f y)
    (let ((x 3))
      (f y)))))
    (g f 2)))


which is also valid Scheme, evaluates to 5 in newLISP, indicating that f gets its value for the free variable x from the binding in g, rather than from the outermost binding in its definition, which is 1. In Scheme (Guile and mzscheme), the same code evaluates to 3, indicating that when f is evaluated inside of g the previous binding of x to 1 is used (static binding). The trend in functional languages these days is going towards static binding, because referential transparency is considered a valuable property, and languages that do dynamic binding make referential transparency all but impossible (e.g. the function f in my example would evaluate to different values in a context that had a different binding for the free variable x). I would be of a mind to question the wisdom of this behavior, seeing as every new functional language since the mid-1980's has made use of static binding. I suppose it must be easier to implement, as Richard Stallman has often said (of Emacs LISP, which also uses it), but are there other more pressing reasons for this design decision?

newdep

#1
Well i always avoid discussion on what is good and what is handy because its basicly

what your used to use... So i wont comment on this ;-)



But "Lutz" has always his point of view ready on certains compares and new logic ;-)





btw nice blog you have there  ;-)
-- (define? (Cornflakes))

Lutz

#2
newLISP does dynamic binding inside of static isolated namespaces.



Personally I find dynamic binding more natural. I believe the current trend towards static binding is more a reflection of today's CS curriculum, than of the realities of programming in the real world of applications outside academia. I see static binding more as a fashion than something which is superior to dynamic binding. Both have their advantages and disadvantages.



For most users the fact of dynamic binding in newLISP is not an issue. But dynamic binding dictates a different programming style, avoiding non-local variables. In an example as yours it is still Ok, if you know how dynamic bindng behaves, because the functions involved are close to each other.



Bigger newLISP programs are typically organized in smaller modules inside contexts/namespaces. The overhead of the namespace mechanism is minimal and there is no speed disadvantage. Namespaces isolates modules from each other and can have state.



You also can use contexts/namespaces to create functions with state and with some special advantages as explained below.



Here is a small example also using the concept of default functions in newLISP:



(context 'gen)

(define (gen:gen x)
    (if acc
        (inc 'acc x)
        (set 'acc x)))

(context MAIN)

(gen 1)  => 1
(gen 1)  => 2
(gen 2)  => 4
(gen 3)  => 7


In newLISP everything (data, functions, contexts, symbols) can be serialized into an ASCII readable file. Imagine you have above example program and executed (gen 1) a few times. Simply by executing (save "myfile" 'gen) you have saved the current state of your name space to a human readable file, which looks like this:



(context 'gen)

(set 'acc 7)

(define (gen:gen x)
  (if acc
   (inc 'acc x)
   (set 'acc x)))


(context 'MAIN)


Now exiting and restarting newLISP you can do a (load "myfile") and have the same state of your function 'gen' back. This is something not possible in a Scheme interpreter unless you save some special binary image format, or invent special language facilities to express the enclosed state of a lambda closure.



newLISP's load/save facility is frequently used for small databases, configuration files, etc.



Please explore newLISP a bit more, you have to unlearn certain ways learned from Common LISP and Scheme, but gain a very agile powerful tool with more depth then suggested by it's small size.



These links from the newlisp.org site may also be useful for you:



http://newlisp.org/index.cgi?page=Differences_to_Other_LISPs">http://newlisp.org/index.cgi?page=Diffe ... ther_LISPs">http://newlisp.org/index.cgi?page=Differences_to_Other_LISPs



and about newLISP's unique automatic memory management:



http://newlisp.org/MemoryManagement.html">http://newlisp.org/MemoryManagement.html



Lutz



ps: welcome to the newLISP community



ps: I will comment on your SSL questions in the other thread later