1

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:() => Int):Int = 
 if(n == 0) f()
 else fact(n-1,() => n*f())
fact(7,() => 1)



def incrementer(x:Int)  = y => y+x
incrementer(2)(3)
Nathan Davis
  • 642
  • 1
  • 5
  • 13
user1868607
  • 2,224
  • 14
  • 23

1 Answers1

2

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 () => n * f(). So when the base case (n = 0) is finally encountered, f is () => n * f(). When f is invoked, f is still () => n * f(). So the call to f results in unbounded recursion with itself.

With incrementer, x will be not be defined when y => x + y is called in your example. This is because y => 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.

Nathan Davis
  • 642
  • 1
  • 5
  • 13