


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CMSC 330, Spring 2009, Midterm 2 Practice Problems
A funarg is simply a function argument where the function is either
**1. Passed as a parameter to a function call
a. What value is returned by the invocation of f( ) with static scoping? Explain. 1, since the binding for “a” in the function “f = fun ( ) -> a” refers to the closest lexical value of “a” at the point where the function is declared in the code (1st^ let a). b. What value is returned by the invocation of f( ) with dynamic scoping? Explain. 2, since the binding for “a” in the function “f = fun ( ) -> a” refers to the closest value of “a” in the call stack at the point where the function is actually invoked (2nd^ let a).
Consider the following OCaml code. let app f w = let x = 1 in f w ;; // value of x determined here // for dynamic scoping let add x y = let incr z = z+x in app incr y;; // value of x determined here // for static scoping (add 2 3) ;;
c. What is the order of invocation for the functions app, add, and incr when evaluating the expression (add 2 3)? 1) add, 2) app, 3) incr incr is defined in add but not invoked until reaching the body of app (as f). d. What value is returned by (add 2 3) with static scoping? Explain. 5, since the binding for x in the function incr refers to the closest lexical value of x (add x ) at the point where the function is declared in the code. e. What value is returned by by (add 2 3) with dynamic scoping? Explain. 4, since the binding for x in the function incr refers to the closest value of x in the call stack ( let x = 1 ) at the point where the function is actually invoked (by app f w … in f w).