Can you give me some examples of tasks unsuited for dynamically scoped lisps? I'm failing to see how the Lexical scope is so much better and not just a matter of changing the coding style, so I'd love to code something and see it with my own eyes.
Thanks!
Under dynamic scoping, a variable is bound to the most recent value assigned to that variable, i.e., the most recent assignment during the program's execution. In the example above, the free variable x in the body of f is evaluated when f(0) is called on line 5.
Dynamic scoping creates variables that can be called from outside the block of code in which they are defined. A variable declared in this fashion is sometimes called a public variable.
Each time a new function is executed, a new scope is pushed onto the stack. Perl supports both dynamic and static scoping. Perl's keyword “my” defines a statically scoped local variable, while the keyword “local” defines a dynamically scoped local variable.
(define (add n) (lambda (m) (+ n m)))
(define add4 (add 4))
(map (add 7) (list 1 2 3))
But that's just one tiny random example. If you dig far enough, you'll find several tons of additional reasons. For a more thorough discussion, you should really go through some textbook. My recommendation for this would be PLAI.
Here's one more demonstration:
(define tax 0.17)
(define (add-tax amt) (+ amt (* amt tax)))
It looks like add-tax
is a function that returns the given amount with the correct tax rate added -- but you can never rely on this being the case. For example, it could be called like this:
(let ((tax -0.17)) (add-tax 100))
and you'd get completely wrong answers. But things are even worse if your language is truly dynamically scoped: you cannot rely on any binding, including functions. Consider this:
(let ((+ -)) (add-tax 100))
And BTW Elisp and CL don't suffer from this problem so directly, using things like a double namespace, and rules about shadowing "built in" bindings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With