;;; Consider the library routine (define sigma (lambda (f lo hi) (define lp (lambda (i sum) (if (> i hi) sum (lp (+ i 1) (+ sum (f i)))))) (lp lo 0))) ;;; And its use: (define sum-powers (lambda (lo hi n) (define nth-power (lambda (x) (expt x n))) (sigma nth-power lo hi))) ;;; Notice that the variable n, which is free ;;; in nth-power is bound in sum-powers. ;;; This is Static Lexical Binding ;;; Why write it this way? ;;; Wouldn't it be better to write it: (define nth-power (lambda (x) (expt x n))) (define sum-powers (lambda (lo hi n) (sigma nth-power lo hi))) ;;; Here, because nth-power is used during ;;; the execution of sum-powers we might like ;;; it to capture the binding of n. Nice ;;; idea... perhaps more modular? ;;; This is one argument for Dynamic Binding. ;;; Unfortunately, it screws up our use of ;;; the library routine. Suppose the author ;;; of the library routine used n instead of ;;; i as his counter variable (define sigma (lambda (f lo hi) (define lp (lambda (n sum) (if (> lo hi) sum (lp (+ n 1) (+ sum (f n)))))) (lp lo 0))) ;;; Then since f=nth-power is called inside ;;; the dynamic extent of the variable n ;;; bound by the lp procedure, the value of n ;;; in nth power will be the value from lp, ;;; not the value from sum-powers... UGH! ;;; We want it to be true that if sigma was ;;; written separately from sum-powers, for ;;; example as part of a library, then the ;;; person who uses sigma need not know the ;;; names that sigma uses in its internal ;;; operation. ;;; Can we accomplish this decomposition, ;;; separating nth-power from sum-of-powers, ;;; with lexical scopes? Yes, as follows: (define nth-power (lambda (n) (lambda (x) (expt x n)))) (define sum-powers (lambda (lo hi n) (sigma (nth-power n) lo hi))) ;;; A bit pedantic, but correct. ;;; On the other hand, there are sometimes ;;; powerful arguments for dynamic binding: ;;; There are often status variables in a ;;; system, such as the open files, or the ;;; exception handler for floating-point ;;; overflow, that really go with extent ;;; rather than scope. That is why we also ;;; provide fluid binding for some lexically ;;; apparent variables.