This review sheet is intended to help you study for the midterm exam. It is not intended to be an exhaustive list of everything we've covered, but it should help you identify the "big ideas". You should also review your lecture notes and the solutions to your homework problems. The lecture "quizzes" are especially useful in helping you study for the exam.
You may bring to the exam a 8.5 by 11 sheet with your own writing (only) on both sides.
Scheme> (foldr (lambda (X, Y) (+ X Y)) 0 '(1, 2, 3, 4))You should assume that foldr "associates" (orders operations) like this: 1 + 2 + 3 + 4 = 1 + (2 + (3 + (4 + 0))). (Notice that the 0 at the end is the unit for addition.) Write the foldr function from scratch. That is, your implementation my use Scheme's list notation and recursion, but no built-in functions. Notice that foldr should not be written exclusively to handle addition! It's first argument can be any function of two variables, its second argument is the unit for that function, and its third argument is a list of elements of the type operated on by the given function.
10
You have been hired by Napquest, a company specializing in finding shortest paths between given cities. (It's called Napquest, because their algorithms tend to be inefficient, causing the users to nap while they wait for the answer!)
A map is a list of "triplets", where each triplet is a list of the form [City1, City2, Distance]. City1 and City2 are just names of cities (e.g. they might be numbers or strings) and Distance is a positive real number indicating the length of the one-way road from City1 to City2. For example, here is a valid map: [ ["Claremont", "Spamville", 1], ["Spamville", "Claremont", 2], ["Spamville", "Foobarsburg", 2], ["Foobarsburg", "Claremont", 4] ]. Notice that the map may contain cycles and that there may be funny things like a one-way road from Claremont to Spamville which is shorter than the one-way road from Spamville to Claremont.
Your job is write a program called tour
which
takes as
input two things: The name of the start city, and an entire map. The
function returns a boolean that indicates whether there is a path
starting in the start city that tours all of the cities (it does not
need to return to the start city).
The program need not be fast, but it must compute the
right answer. For full credit, keep your code very short.
However you may find it easier to write a helper function.
In addition to being able to translate into Hmmm, make sure you can also answer questions about provided Hmmm code such as:
(define (main)
(let* ((x (get input from user))
(y (get input from user))
(result (foo x y)))
(display result)))
(define (foo x y)
(let* (
(z 2)
(a (smallestfactor x z))
(b (smallestfactor y z)))
(if (> a b)
x
y)))
(define (smallestfactor n f)
(if (= 0 (mod n f))
f
(smallestfactor n (+ f 1))))