Claimed problems with Lisp dynamic scoping What would be the problem of running the following scala codes assuming we are working with dynamic scoping and a global stack to store environments as some variants of Lisp do?

def fact(n:Int,f:() =&gt; Int):Int = 
 if(n == 0) f()
 else fact(n-1,() =&gt; n*f())
fact(7,() =&gt; 1)



def incrementer(x:Int)  = y =&gt; y+x
incrementer(2)(3)
With fact, the primary problem is with f.  If n is 0, then it works fine.  However, for any other value of n, fact is called with an f of () =&gt; n * f().  So when the base case (n = 0) is finally encountered, f is () =&gt; n * f().  When f is invoked, f is still () =&gt; n * f().  So the call to f results in unbounded recursion with itself.

With incrementer, x will be not be defined when y =&gt; x + y is called in your example.  This is because y =&gt; x + y does not close over x (because of dynamic, as opposed to lexical, binding), there is no call on the stack that binds x to a value when the closure is invoked, and there is no global / top-level binding for x.