Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, \(f\), that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to \(\mathrm{mf}\) is the special symbol how-many-calls?, then \(\mathrm{mf}\) returns the value of the counter. If the input is the special symbol reset-count, then \(m f\) resets the counter to zero. For any other input, \(m f\) returns the result of calling \(f\) on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure: (define s (make-monitored sqrt)) \((\mathrm{s} 100)\) 10 \(\left(\mathrm{~s}^{\prime}\right.\) hou-many-calls?) 1

Short Answer

Expert verified
Implement `make-monitored` to wrap a function with a call counter and special commands.

Step by step solution

01

Understand the Task

We are tasked with creating a procedure, `make-monitored`, that takes another procedure `f` as an input. The aim is to track how many times this procedure is called.
02

Create a Counter

Inside `make-monitored`, we need to create a counter variable initialized to zero. This counter will track the number of times the procedure is called.
03

Define the Monitored Function

Within `make-monitored`, define a new procedure, `mf`, which will handle different types of inputs, including the special symbols for checking and resetting the counter.
04

Handle Special Inputs

In the function `mf`, check if the input is the symbol `how-many-calls?`. If so, return the current value of the counter. If the input is `reset-count`, set the counter back to zero.
05

Handle Regular Function Calls

For any other input, `mf` should call procedure `f` with this input, increase the counter by one, and return the result of the procedure call.
06

Return the Monitored Function

Finally, ensure `make-monitored` returns the `mf` function as the output, closing over the counter to maintain its state.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Procedure Monitoring
Procedure monitoring is an essential technique in software testing, where the goal is to observe and track the activity of specific functions during a computation. This involves creating a wrapper or modified version of a procedure that can count and record each time it is executed. By implementing procedure monitoring, developers can better understand how often certain functions are called, which can be useful for debugging, optimizing, and ensuring that the software behaves as expected. One common approach to procedure monitoring is to develop a high-order function, such as `make-monitored` in our example. This function takes another function as its parameter and returns a new procedure that not only calls the original function but also includes functionality to track its usage. With this, procedure monitoring becomes a powerful tool in the developer's toolkit for identifying unexpected behaviors and potential inefficiencies.
Function Calls
In programming, a function call is when a function is executed or invoked. It's an essential mechanism that allows you to run reusable blocks of code throughout your program. In the context of procedure monitoring, each time you call the monitored function, it's crucial that the call is recorded correctly. Function calls typically involve providing arguments to the function. For example, when you call a function designed to calculate a square root, you provide a number as the input. In our exercise, whenever the `mf` procedure is called with an argument that isn't a special instruction (like `how-many-calls?` or `reset-count`), it treats the argument as a typical function input, increments the call counter, and then returns the result of the function call. By systematically tracking function calls using a counter within the monitored function, developers can gain insight into function usage patterns and identify potential areas for performance enhancement.
Counter Variables
Counter variables are simple yet powerful tools in programming, often used to track how many times an event occurs. In the procedure monitoring scenario, a counter variable is employed to keep a tally of how often the monitored procedure is invoked. The counter is usually initialized to zero at the start. Every time the monitored procedure is called, the counter is incremented. If the special input `how-many-calls?` is received, the counter’s value is returned, showing the total number of calls made so far. Similarly, if the input `reset-count` is received, the counter is reset to zero. Using counter variables in the described manner simplifies the process of tracking occurrences without having to write more complex logic. It allows for a clear understanding of function usage frequency, which is especially helpful in testing and debugging phases.
Scheme Programming
Scheme is a minimalist, elegant dialect of Lisp, emphasizing functional programming. It is widely used in academic settings to teach programming concepts due to its simple syntax and powerful abstractions. In the context of the exercise, Scheme is used to create a function that monitors procedures. In Scheme, functions are first-class citizens, meaning they can be passed as arguments to other functions, returned as values, and be part of lists. This characteristic makes it straightforward to create higher-order functions like `make-monitored`. Such functions take advantage of Scheme's capabilities to modify or enhance functions by adding additional behavior—like counting calls. One key feature of Scheme that supports our exercise is lexical scoping, which ensures that a variable (like the counter) retains its state within the scope it's defined. This inherent trait of Scheme makes it ideal for creating stateful functions, such as those required for monitoring procedures. Understanding these Scheme principles is crucial for effectively leveraging the language in functional programming tasks.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2,3 , or 5 . One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2,3 , and 5 . But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, let us call the required stream of numbers \(S\) and notice the following facts about it. \- S begins with \(1 .\) \- The elements of (scale-stream S 2) are also elements of S. \- The same is true for (scale-stream \(\mathrm{S} 3\) ) and (scale-stream \(5 \mathrm{~S}\) ). \- These are all the elements of \(\mathrm{S}\). Now all we have to do is combine elements from these sources. For this we define a procedure merge that combines two ordered streams into one ordered result stream, eliminating repetitions: (define (merge s1 s2) (cond ((stream-null? s1) s2) ((stream-nul1? s2) s1) (else (let ( s1car (stream-car s1)) (s2car (stream-car s2))) (cond ((< s1car s2car) (cons-stream s1car (merge (stream-cdr s1) s2))) ((> s1car s2car) (cons-stream s2car (merge s1 (stream-cdr s2)))) (else (cons-stream s1car (merge (stream-cdr s1) Then the required stream may be constructed with merge, as follows: (define \(\mathrm{S}\) (cons-stream 1 (merge \(\langle ? ?\rangle\langle ? ?\rangle)\) ) Fill in the missing expressions in the places marked \(\langle ? ?\rangle\) above.

The procedures to be run during each time segment of the agenda are kept in a queue. Thus, the procedures for each segment are called in the order in which they were added to the agenda (first in, first out). Explain why this order must be used. In particular, trace the behavior of an and-gate whose inputs change from 0,1 to 1,0 in the same segment and say how the behavior would differ if we stored a segment's procedures in an ordinary list, adding and removing procedures only at the front (last in, first out).

Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in (define acc (make-account 100 'secret-password)) The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint: ((acc' 'secret-password 'withdraw) 40) 60 ((acc' 'some-other-password 'deposit) 50) "Incorrect password"

Instead of representing a queue as a pair of pointers, we can build a queue as a procedure with local state. The local state will consist of pointers to the beginning and the end of an ordinary list. Thus, the make-queue procedure will have the form (define (make-queue) (let ((front-ptr ...) (rear-ptr ...)) (definitions of internal procedures) (define (dispatch m) ...) dispatch)) Complete the definition of make-queue and provide implementations of the queue operations using this representation.

Give all possible values of \(x\) that can result from executing (define \(x\) 10) (parallel-execute (lambda () (set! \(x(* x x))\) ) (lambda () (set! \(x(* x x x))\) )) Which of these possibilities remain if we instead use serialized procedures: (define \(x\) 10) (define s (make-serializer)) (parallel-execute (s (lambda () (set! \(x(* x x))\) )) )) (s (lambda () (set! \(x(* x x x))\) )

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free