Question

In: Computer Science

Dynamic scope is an alternative to passing parameters or mutating global variables. The values of local...

Dynamic scope is an alternative to passing parameters or mutating global variables. The values of local variables can be “passed” from function to function through dynamically embedded scopes. For the following lines of Lisp, state your guess for the output before running it, and then run it. Explain what you see by drawing a call stack trace:

(defvar *x* 0)

(defun main () (foo) (print *x*))

(defun foo () (let ((*x* 1)) (bar) (print *x*)))

(defun bar () (incf *x*) (bazz))

(defun bazz () (incf *x*) (baz))

(defun baz () (incf *x*))

(main)

Solutions

Expert Solution

The code first prints 4 and then 0. Here is the call stack trace:

X = 0
        Entered main ()
                Entered foo()
                        LET X = 1    ------- creates a new binding for X, applicable only while inside foo ()
                        Entered bar ()
                                X incremented to 2
                                Entered bazz ()
                                        X incremented to 3
                                        Entered baz ()
                                                X incremented to 4
                                        Exited baz ()
                                Exited bazz ()
                        Exited bar ()
                        print X ------------------------> prints as 4, the value of the global variable at this time
                Exited foo (value of X assigned via LET on entering foo is no longer applicable)
                <Implicitly, X takes on the older value before entering foo> X = 0
                print X  ----------------------> prints as 0
        Exited main ()
        

Related Solutions

When are values passed as parameters? a. When parameters are primitives or wrappers b. When parameters...
When are values passed as parameters? a. When parameters are primitives or wrappers b. When parameters are user-defined objects c. When parameters are objects What is the result of a == b? public static void main(String[] args) { int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; } a. True b. False c. Compile Error
3. Project variables: Each project is constrained by the variables of schedule, budget, and scope. As...
3. Project variables: Each project is constrained by the variables of schedule, budget, and scope. As part of the change management process, a change to the scope of the project is requested. How does this change in scope affect the project schedule and budget? 4. Project management competencies: If an organization hires an experienced project management contractor to lead a project, which project management competencies must this individual develop to become an effective project manager in the organization?
Comment on how to use subroutines on the ATMega328P. Comment on the ways of passing parameters,...
Comment on how to use subroutines on the ATMega328P. Comment on the ways of passing parameters, returning results and how the processor manages subroutine calls, possibly recursive calls. Also comment on the role of the stack in the use of subroutines.
The Classical School and the identification of a local/global problem. a) Identify a local/global problem that...
The Classical School and the identification of a local/global problem. a) Identify a local/global problem that might be caused by the application of a policy that is based on a theory or concept of the Classical school. Your answer needs to provide at least three paragraphs. The first paragraph identifies and explains in your own words the concept or theory of the Classical School The second paragraph identifies and explains the a local/global problem. The third paragraph explains the connection...
C language Modify the getAvg() function to support passing an array by reference. The parameters of...
C language Modify the getAvg() function to support passing an array by reference. The parameters of this function should be pointer to a float array, and the size of the array. Returns the average of the array. Build a function called getScoreFromUser() with the following properties: Asks the user to enter the score scans into a local float variable returns the float value that the user entered. Build and present a menu to a user like the following and enclose...
What are global variables, how are global variables used in Arm architecture. Please feel free to...
What are global variables, how are global variables used in Arm architecture. Please feel free to use your own examples.
For C++ Make a function that swaps the values that change the values between the parameters...
For C++ Make a function that swaps the values that change the values between the parameters as the following rule. The functions swap takes 2, 3 or 4 parameters and these functions are implemented as the overloading functions. swap(int &, int &) : change the values each other. swap(int &, int &, int &) : change the value with the order. [min, medium, max] swap(int &, int &, int &, int &) : change the value like circular-shift-right [A, B,...
Go into detail about the scope and purpose of a global marketing strategy
Go into detail about the scope and purpose of a global marketing strategy
1. Null and alternative hypotheses are statements about A. population parameters. B. sample parameters. C. sample...
1. Null and alternative hypotheses are statements about A. population parameters. B. sample parameters. C. sample statistics. D. population statistics. E. It depends on the situation and the type of data has been collected. 2. A hypothesis test is conducted under the initial assumption that A. the alternative hypothesis is false. B. the alternative hypothesis is true. C. the null hypothesis is false. D. the null hypothesis is true. E. We never make any assumptions when conducting a hypothesis test....
Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function,...
Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function, returning a pointer by a function, dangling pointer, and memory deallocation, pointer initialization, and struct data type. Project description In this project, you will create a database of employees of an organization while meeting the requirements described below. Your program MUST NOT interact with the user to receive inputs, so that the instructor and/or the teaching assistant can save a big time in testing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT