Page 1 of 1

Why dynamic scope?

Posted: Thu May 03, 2012 3:42 am
by arquebus
I cant believe no one has asked this yet, but why does NewLisp have dynamic scope? I dont see any benefit to dynamic scope as all it does is create danger for name clashes between functions although I did notice that their is lexical scope for namespaces and modules. Is the reason for avoiding lexical scope is that you want to avoid incuring an overhead on speed in NewLisp? Is there any practical reason for having dynamic scope over lexical scope other than speed and efficiency of the running code?

Re: Why dynamic scope?

Posted: Thu May 03, 2012 6:15 am
by Lutz
The dangers of dynamic scope are vastly overrated and because of that, possibilities of dynamic scoping are almost unexplored.

Over the years, we have hardly ever seen dynamic scope to be a problem. Where the danger is somewhat present, is in 'define-maccro' fexprs and that danger is easily avoided using namespaces. There are very few reasons in newLISP style of programming to pass quoted symbols to 'define' functions, there are other safe ways to pass data by reference in newLISP, i.e. via namespace handles.

You can have lexical isolation using namespaces, which have very minimal overhead in newLISP - you can literally have millions of them. 'define-macro' style fexprs can be put in their own namespace context and then are completely safe. Generally, the best style of newLISP programming is, to put related data, functions and fexprs into a namespace context.

The absence of Scheme like lexical closures lets newLISP do a different kind of automatic memory management - not traditional garbage collection, but a synchronous type of memory management - much faster, without unexpected garbage collection pauses and much more efficient in using memory resources. There is no way to write a fully dynamic language that small and fast as newLISP using traditional garbage collection memory management.

see also:

http://www.newlisp.org/index.cgi?Closures
http://www.newlisp.org/MemoryManagement.html
http://www.newlisp.org/ExpressionEvaluation.html

these links all referenced from here:

http://www.newlisp.org/index.cgi?page=D ... ther_LISPs

Re: Why dynamic scope?

Posted: Thu May 03, 2012 8:00 am
by cormullion
There have been quite a few discussions about this topic here on the forum, over the years. Also, there are some interesting posts on other blogs, such as Kazimir's: http://kazimirmajorinc.blogspot.co.uk/2 ... scope.html.

You may find that dynamic scope is not a problem for your scripting needs, or proves advantageous. Or you may find it a huge stumbling block...

Re: Why dynamic scope?

Posted: Thu May 03, 2012 3:04 pm
by Lutz
Just to point out the essential in Kazimir's post:

Code: Select all

(define (sum f n)
    (set 'result 0)
    (for (i 1 n)
        (inc result (f i)))
    result)

; works because no clash of free j in f with i in sum
(for (j 1 5)
    (define (f x) (pow x j))
    (println j ": " (sum f 10)))

; does not work, free i in f clashes with i in sum
(for (i 1 5)
     (define (f x) (pow x i))
     (println i ": " (sum f 10)))

; works with previous expansion of free variable
(for (i 1 5)
     (letex (e i) (define (f x) (pow x e))) ; expansion of free variable 
     (println i ": " (sum f 10)))
produces:

Code: Select all

1: 55
2: 385
3: 3025
4: 25333
5: 220825
1: 1.040507132e+10
2: 1.040507132e+10
3: 1.040507132e+10
4: 1.040507132e+10
5: 1.040507132e+10
1: 55
2: 385
3: 3025
4: 25333
5: 220825
Free variables in dynamic scoping are not pre-bound during function definition. But just like a Scheme closure would bind free variables to its environment during definition of f, the same can be done using 'letex' or 'expand' binding the free variable during function definition.

Re: Why dynamic scope?

Posted: Thu May 03, 2012 9:10 pm
by arquebus
Lutz and Cormullian- Thank you for your responses. Although I was hoping for a quick 2 sentence explanation, the fact that the reason for using dynamic scope requires a deeper understanding of Lisp is of course a good thing. I have also discovered PicoLisp which is another light weight Lisp scripting language which is also dynamically scoped. Incidentally it was also designed by a german fellow. I also plan to learn emacs which means I will learn elisp, so I am now much more motivated to thoroughly understand elisp now that I have discovered that dynamic typing is used in NewLisp and PicoLisp.

I myself enjoy learning programming languages as a hobby so I have a general understanding of Lisp syntax as well as most other major functional languages. So hopefully I know enough to be able to understand the articles youve linked to.

I have applied to some universities and hopefully I will be accepted by one and start my CS degree this fall.
thanks much

Re: Why dynamic scope?

Posted: Sat Jun 02, 2012 2:39 pm
by Ishpeck
arquebus wrote:I also plan to learn emacs which means I will learn elisp, so I am now much more motivated to thoroughly understand elisp now...
emacs is my very best friend.

Be sure to get newlisp mode for emacs.
http://www.artfulcode.net/projects/

It's delightful.

Re: Why dynamic scope?

Posted: Thu Jun 09, 2016 2:50 am
by abaddon1234
the fact that the reason for using dynamic scope requires a deeper understanding of Lisp is of course a good thing.


gclub online

Re: Why dynamic scope?

Posted: Fri Jun 24, 2016 5:55 pm
by rickyboy
abaddon1234 wrote:the fact that the reason for using dynamic scope requires a deeper understanding of Lisp is of course a good thing.
Nice plagarism. All you did was copy exactly what arquebus wrote two posts above yours.
abaddon1234 wrote: gclub online
Again, you should be banned from this forum.

Re: Why dynamic scope?

Posted: Fri Dec 02, 2016 10:13 am
by clubZa
I understand what you want

gclub download

Re: Why dynamic scope?

Posted: Fri Dec 02, 2016 5:49 pm
by TedWalther
We have a flood of spammers bumping topics. But anyway... wow. artfulcode is gone. :(