Tutorial 1: Review (May 4)This tutorial covers the basic the three basic tools in Scheme that we will be using in this course: Functions as values, Local expressions, and Elementary mutation. These concepts were introduced in Lecture Module 1 (see Handouts).
Suppose we want a function to compute the value of the polynomial f(x) = x^{4} + 8x^{3} + 25x^{2} + 36x + 20at a given point x. We could simplify this computation by noting that f(x) factors to f(x) = (x+2)^{2} * ((x+2)^{2} + 1) Using this simplification, and using a Say we have a global variable Write a function,(define sum 0) addtosum , which consumes a number and updates the value of
sum to be the previous value of sum plus the given
number. The function should produce the new value of sum .
Write a function which consumes a 2ary function (that is, a function which
takes two arguments), and a single value, and produces the result of applying
that function to the given argument repeated twice. So, for example,
the line One way to write a name is as a list of two strings, for example
So, for example, if we have: then the result would be:(define addbush (makeaddsurname "Bush")) (addbush "George") (addbush "Laura")
The following exercises will require you to combine the three tools described above to write some more complex functions. We want a function which takes no arguments and produces, on consecutive calls,
the next integer in increasing order. Your task is to write a function,
So, for instance, if we execute the following code: then the following numbers should be produced:(define nextfrom5 (makenextinteger 5)) (nextfrom5) (nextfrom5) (nextfrom5) 5 6 7 Professors, TAs, tutors, and the like often need to enter a list of marks and
then find the average. We want a function that consumes one score and produces
the average of all scores entered so far. We may want to do this many times,
so using a global variable is undesirable. Rather, write a function,
Recall the formula for computing the average of n scores s_{1}, s_{2},...,s_{n}: average = (s_{1} + s_{2} + ... + s_{n})/n So if we have the following code: then the following will be produced:(define avg (makerunningaverage)) (avg 70) (avg 80) (avg 90)
Hint: the most elegant solution actually stores two local variables. We want a function that takes no arguments and returns each element of
some list in order, in successive calls. When there are no more elements in the
list, the function should just return So the following code: would produce the following:(define iterator (makeiterator (list 'a 'b))) (iterator) (iterator) (iterator)


Last modified on Friday, 19 August 2011, at 18:05 hours.