2
votes

Assuming dynamic scoping, what would be the difference in the display statement if it was shallow or deep binding? I understand that shallow binding is when it is bound at the call and deep is when it is bound while passed as a parameter but I'm not sure how this works in the context of scheme. I think that the deep binding should print 2 but I'm not sure

(define A 
    (let* ((x 2)
           (C (lambda (P) 
                 (let ((x 4))
                    (P))))
           (D (display x))
           (B (let ((x 3))
                   (C D))))
        (B)))
1
Possible duplicate of Scheme and Shallow Binding - molbdnilo
@greenteam could you give your sources perhaps for your interpretation of what shallow/deep binding is? Also, Scheme is lexical, so maybe you meant lisp in general and if so should this be retagged as "lisp" perhaps? - Will Ness
it was wrong to make that edit as it changed the post too much, so I rolled back. - Will Ness
@WillNess entirely honestly this question is from when I was taking a functional programming class in college 3 years ago and I haven't looked at the material since the class ended. I'd be lying if I said I remember the difference well enough to explain it. If I remember correctly, deep binding is taking on the scope of the parent function of the function in question and shallow binding is taking the scope of the last function to call the function in question. (apologies if that wording was confusing) - greenteam
thanks. btw, the reference is Shallow Binding in LISP 1.5. - Will Ness

1 Answers

0
votes

Let's get some definitions into play:

  • Deep binding binds the environment at the time a procedure is passed as an argument.
  • Shallow binding binds the environment at the time a procedure is actually called

So the only difference will be regarding the evaluation of C which happens inside B.

(define A 
    (let* ((x 2)
           (C (lambda (P) 
                 (let ((x 4))
                    (P))))
           (D (display x))
           (B (let ((x 3))
                   (C D))))
        (B)))

On the other hand, the code itself seems to be buggy, (display x) will not return a value yet it is passed into C.

Let's assume you meant (D (lambda () (display x)))

(define A 
    (let* ((x 2)
           (C (lambda (P) 
                 (let ((x 4))
                    (P))))
           (D (lambda () (display x)))
           (B (let ((x 3))
                   (C D))))
        (B)))

under deep binding the value of x is 3 when D is passed into C, so we should expect 3 to be printed.

under shallow binding the value of x is 4 when the procedure is actually called, so 4 will be printed.