A shop calculates an order quote using a pure function: it returns the same result for the same input and produces no observable side effects. For a positive integer quantity qqq, its quote in dollars is
The application adds the quotes for three separate orders by evaluating
quote(4) + quote(4) + quote(9)
Compare two implementations. Without a cache, every call evaluates the function body. With memoization, previously computed results are stored by input: a repeated call returns the stored result without evaluating the body again. The cache is initially empty, and calls are processed from left to right.
Complete the table with exact integers. Count function-body evaluations, not additions or cache lookups. Each order still contributes its full quote to the total.
Unlock full course notes by signing up
Tracing a loop and a pipeline side by side
A shop applies a 2-dollar discount to every item priced above 5 dollars and totals the discounted prices. Two versions of the code, one procedural and one functional, process the same list of prices.
Complete the table with whole numbers. Values of total are in dollars.
State and side effects in a counter class
Consider this Python code, which defines a class and then makes three method calls on two objects.
Which of the following statements are true? Select all that apply.
Counting Prolog answers for a recursive rule
A Prolog program records which course is a direct prerequisite of another and defines a recursive rule for indirect prerequisites.
The query ?- leads_to(cs101, C). is entered, and the user keeps asking for more answers until Prolog reports there are no more. Prolog uses its standard search: clauses top to bottom, goals left to right, with backtracking. How many answers for C does Prolog report in total, counting any repeated value each time it appears? Give your answer as a whole number.
Finding unknown inputs with a booking relation
A theatre booking system uses a logical relation to describe all combinations of values satisfying its stated conditions. Its relation booking(a,c)\operatorname{booking}(a,c)booking(a,c) holds when aaa is the number of adult tickets, ccc is the number of child tickets, and all of these conditions hold:
aaa and ccc are integers from 1 through 9, inclusive.
Adult tickets cost $18 each and child tickets cost $12 each.