Question

In: Computer Science

Apart from the formal parameter(s) and the body of the function, what else does the closure...

Apart from the formal parameter(s) and the body of the function, what else does the closure
ADT encapsulate? Why is this necessary for correct evaluation of the function at call-time?
Give a concrete example if possible

Solutions

Expert Solution

The encapsulation of Abstract data type is similar like it gathers the data of an object and puts them into a package but the closure ADT is used to encapsulate the constructors's arguments also which act as local state for the procedures.

this form of closure is different from the kind commonly found in functional language like lisp for 2 resons, first it possibly form closure over function but not ercords that is the reason record must be simulated as ann explicit case over a set.

there is a code given below for bettter understading-

nil = recursive self = record
null? = true
head = error;
tail = error;
cons = fun(y) Cell(y, self);
equal = fun(m) m.null?


cell(x, l) = recursive self = record
null? = false
head = x;
tail = l;
cons = fun(y) Cell(y, self);
equal = fun(m) (not m.null?)

above code is only for the understaning the concept that closure encapsulate the constructors arguments.

This kind of encapsulation is necessary because the closure itself has a reference to threshold and it can use that variable each time filter function calls it.This filter function itself might vbe defined in a completely separate file.

the below code will help to understand -

function_derivative(f, dx) {
    return function (x) {
    return (f(x + dx) - f(x)) / dx;

the reason is that , closure in this case outlives the execution of the function that creates it and he variables  f and dx  live on after the function returns derivaives.


Related Solutions

1. What does it mean to pass a parameter when the formal parameter is some type of class?
JAVA QuestionsAnswer both questions explaining what each is and using an example to back up your explanation.1. What does it mean to pass a parameter when the formal parameter is some type of class?2. What does it mean to pass a parameter when formal parameter is some type of interface?
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s...
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s prototype is changed as shown below. Please explain code with comments //-------------------------------------------------- void NextPrime(int *x) //-------------------------------------------------- { bool IsPrime(const int x); }
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
What are the closure properties for CFGs? What does it mean for a CFG to be...
What are the closure properties for CFGs? What does it mean for a CFG to be closed under one of these operations?
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws a square of length s. The square should be filled by a randomized color. Then write another function, that calls the shape function to draw 4 squares as a 2x2 table. Please answer in python.
The average resistivity of the human body (apart from surface resistance of the skin) is about...
The average resistivity of the human body (apart from surface resistance of the skin) is about 5.0 Ω⋅mΩ⋅m. The conducting path between the right and left hands can be approximated as a cylinder 1.6 mm long and 0.10 mm in diameter. The skin resistance can be made negligible by soaking the hands in salt water. If skin resistance is negligible, what potential difference between the hands is needed for a lethal shock current of 100 mAmA? (Your result shows that...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Apart from the gains from comparative advantage, what other benefits does a nation gain from trade?
Apart from the gains from comparative advantage, what other benefits does a nation gain from trade?
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the...
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the function, it asks the user to input scores for 3 items (Each of the items is out of 100). The final score is calculated by 20% of item1 + 30% of item2 + 50% of item3. After calculating the final score, the function returns it to the caller. The scores are floating-point numbers. #Write another function called getLetterGrade() that takes a float parameter and...
What does relational closure mean, and what is its significance in application design? What techniques can...
What does relational closure mean, and what is its significance in application design? What techniques can be used to minimize deadlocks, and how? Compare and contrast performance problems with availability problems. Describe three types of technology that can help alleviate availability issues in a database
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT