CS116 Study Guide - Quiz Guide: Mutation
SchoolUniversity of Waterloo
This preview shows half of the first page. to view the full 2 pages of the document.
Changes to the design recipe to accommodate mutation
Mutation allows for functions to do more than algebraic manipulation of their
arguments. In particular, a function can change the value of a variable, or can have
other side effects.
This is explored in some depth in Section 36.4 of How to Design Programs. These are
the elements of the modified design recipes for programs using mutations:
State variables are the way that we store the values that may change over
the execution of a program. For these variables, if they exist, define them and
give them a value.
The contract should describe the consumed value(s) and the produced
value(s) of the function. Use (void) to indicate that the function has no
consumed values, and use (void) to indicate the function has no produced
The purpose of a function should describe what the function consumes and
Effects are added to explain the side effects of the function, that is, how
calling a function will have other effects besides computing the returned
value. You should also explain what variables are changed after calling the
function, how these variables change, how the consumed values, if any, are
involved with the change.
Examples must be revised to account for time’s passage: the key thing is that
a variable may have a “before” and an “after” value. For example, you need to
provide sample values of variables before the function call, indicate
parameter values, and indicate new value of variables after the function call.
You should provide at least 2 examples per function. For question with
unique input and output values, one example is usually sufficient.
Function body will use begin, set! and/or structure field mutator
functions where appropriate.
Tests: Use check-expect in combination with begin to ensure that
your function works as desired. Use begin to join together all parts of a
single test. As the first part of the begin statement, you may assign the state
variable; the second part of the statement usually involves mutating state
variables (by calling the function); the third part of the statement creates a
list (or single value) with the actual values of all mutated or produced values
to be checked. Finally, use check-expect to compare the last expression
of the begin statement with the expected.
The following solution illustrates how to provide the modified design recipes for
You're Reading a Preview
Unlock to view full version