Check whether expanding a construct preserves effects

A language for a sensor controller includes next(), which consumes and returns the next integer from a queue of readings. The queue initially contains 4, followed by 9.


The specified operation Square(E) evaluates E once, stores the resulting integer, and multiplies that stored integer by itself. A proposed translation replaces Square(E) with Mul(E, E). Multiplication evaluates its operands once each, from left to right, then multiplies their results. Each occurrence of next() makes a separate call.


Run the original expression Square(next()) and the translated expression Mul(next(), next()) separately, resetting the queue before each run. Find each returned integer and the number of calls to next() during each run. Enter all four answers as exact integers; returned values and call counts are dimensionless.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Programming Language Design and Construction Questions: