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) = x4 + 8x3 + 25x2 + 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
local expression to store
an intermediate value, write a function
which consumes a number
produces the value f(x).
Say we have a global variable
sum, which is defined initially
to be zero, with
Write a function,(define sum 0)
add-to-sum, which consumes a number and updates the value of
sumto be the previous value of
sumplus the given number. The function should produce the new value of
Write a function which consumes a 2-ary 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,
(apply-double + 4) should produce the result of
(+ 4 4), which is of course
One way to write a name is as a list of two strings, for example
("Christopher" "Columbus"). We want a function that consumes
a given (i.e. first) name as a string,
and produces a list of that given name and some
surname. Write a function which consumes a surname (string) and produces a
function as described above, to add given names to that surname.
So, for example, if we have:
then the result would be:(define add-bush (make-add-surname "Bush")) (add-bush "George") (add-bush "Laura")
(list "George" "Bush")
(list "Laura" "Bush")
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,
make-next-integer, which consumes one integer (the starting point),
and produces such a function.
So, for instance, if we execute the following code:
then the following numbers should be produced:(define next-from-5 (make-next-integer 5)) (next-from-5) (next-from-5) (next-from-5)
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,
make-running-average, which takes no arguments and produces
a new function as described above, initialized with no scores.
Recall the formula for computing the average of n scores s1, s2,...,sn:
average = (s1 + s2 + ... + sn)/n
So if we have the following code:
then the following will be produced:(define avg (make-running-average)) (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
empty. Such a function is
called an iterator for the given list. Write a function called
which consumes a list and produces a function which is an iterator for that
So the following code:
would produce the following:(define iterator (make-iterator (list 'a 'b))) (iterator) (iterator) (iterator)
Last modified on Saturday, 11 August 2018, at 22:52 hours.