Assigned: Wednesday, September 14
Due: Tuesday, September 20 by 10:30pm
Please read the exam procedures page for policies, turn-in procedures, and grading details. If you have any questions about the exam, check the Q&A at the bottom of this page. We will generally spend some time in class every day on questions and answers while the exam is in progress.
While the exam is out, please check back periodically to see if we have reported any new errata.
Complete the exam using the exam1.rkt starter source code. Please rename this file to 000000.rkt, but replace 000000 with your assigned random number.
Topics: numeric values, writing procedures
Write, but do not document, a procedure (num-largest x y z) that takes three numbers x, y, and z as input, and returns the number of values that are tied for largest value. Your procedure does not need to work if any of the parameters are zero or negative. The following example outputs may be helpful:
> (num-largest 100 4 2)
1
> (num-largest 50 50 50)
3
> (num-largest 5 2 2)
1
> (num-largest 1 3 3)
2
Note: You may use lambda, section, and compose to implement this procedure, but you may not use if, which we have not yet covered in class.
Topics: numeric values, writing procedures, section, compose
Without using lambda, write and document a procedure (bound-rating rating) that takes one input, a real number that we’ll call rating, and returns:
rating < 0rating > 10rating, if neither of the two previous conditions
holds.Note that to define this procedure, you will find it necessary to use compose, section, or both. You will also find it useful to go back and think about how we’ve bounded values in past exercises. You may not use if, since we have not yet covered this in class.
Topics: irgb colors, image-variant
Consider the following procedure documentation.
;;; Procedure:
;;; irgb-boost-dominant
;;; Parameters:
;;; color, an integer-encoded RGB color
;;; Purpose:
;;; Adds 32 to each of the dominant RGB components of color
;;; Produces:
;;; boosted, an integer-encoded RGB color
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; Let max_value = (max (irgb-red color) (irgb-green color) (irgb-blue color))
;;; Let max_boosted = (min 255 (+ max_value 32))
;;; If (irgb-red color) = max_value, then (irgb-red boosted) = max_boosted,
;;; If (irgb-green color) = max_value, then (irgb-green boosted) = max_boosted,
;;; If (irgb-blue color) = max_value, then (irgb-blue boosted) = max_boosted,
;;; If (irgb-red color) < max_value, then (irgb-red boosted) = (irgb-red color),
;;; If (irgb-green color) < max_value, then (irgb-green boosted) = (irgb-green color),
;;; If (irgb-blue color) < max_value, then (irgb-blue boosted) = (irgb-blue color)
Here is an example of running irgb-boost-dominant on our favorite kitten image:
> (define kitten (image-load "/home/rebelsky/Desktop/kitten.jpg"))
> (image-show kitten)

> (define boosted-kitten (image-variant kitten irgb-boost-dominant))
> (image-show boosted-kitten)

a. Implement the procedure described above. You may use helper procedures for this problem (and is recommended!).
Note: You may use lambda, section, and compose to implement this procedure, but you may not use if, which we have not yet covered in class.
b. Test your procedure on at least two images (using image-variant) and a minimum of three RGB colors.
Topics: irgb colors, section, compose, lambda
Suppose we wish to write a procedure (irgb-granular color) which takes as input an integer-encoded RGB color and outputs a more granular version of the color. In particular, irgb-granular rounds each RGB component down to the nearest multiple of 32. Below are some sample executions of the procedure.
> (irgb->string (irgb-granular (irgb 32 100 70)))
"32/96/64"
> (irgb->string (irgb-granular (irgb 255 0 150)))
"224/0/128"
a. Write, but do not document, the irgb-granular procedure using lambda and name it irgb-granular-a. You may not use section nor compose for this part. You may write helper procedures provided you follow the same guidelines for the helper procedures.
When writing a procedure to transform an integer-encoded RGB color, we commonly find ourselves needing to apply the same modifications to each RGB component of the color. Below is the definition of a function (irgb-apply-proc color proc) which takes as input an integer-encoded RGB color and a procedure as parameters and applies the procedure proc to each RGB component of color.
;;; Procedure:
;;; irgb-apply-proc
;;; Parameters:
;;; color, an integer-encoded RGB color
;;; proc, a procedure that takes and returns an integer
;;; Purpose:
;;; Make a new irgb color where each component is the result of applying proc to the
;;; corresponding component of color
;;; Produces:
;;; result, an integer-encoded RGB color
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; (irgb-red result) = (proc (irgb-red color)) bounded between 0 and 255
;;; (irgb-green result) = (proc (irgb-green color)) bounded between 0 and 255
;;; (irgb-blue result) = (proc (irgb-blue color)) bounded between 0 and 255
(define irgb-apply-proc
(lambda (color proc)
(irgb (proc (irgb-red color))
(proc (irgb-green color))
(proc (irgb-blue color)))))
b. Write, but do not document, the irgb-granular procedure using section and compose and name it irgb-granular-b. You may make use of the irgb-apply-proc definition above, but you may not use lambda in any other way in this part. You may write helper procedures provided you follow the same guidelines for the helper procedures.
Topics: documentation, code reading
Sometimes students (and professors) come up with difficult-to-read solutions to arithmetic problems, like the one below.
(define hamster (lambda
(cat dog) (- (/ (+
(min cat dog) (max
cat dog)) 2) (min
cat dog))))
a. Add (or remove) carriage returns and indent the code so that it is formatted clearly.
b. Rename the procedure and the parameters so that they will make sense to the reader.
c. Write 6P-style documentation for the code.
d. Explain how the code achieves its purpose.
Topics: I/O, how scheme evaluates expressions
The following procedure computes one of the two outcomes of the quadratic formula:
(define quadratic1
(lambda (a b c)
(/ (+ (- b) (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a))))
As you saw in lab, we can write our own versions of the procedures used in this expression, such as sqrt_* and multiply_*, that compute the same value but also produce output in the interactions pane to tell us when they execute. This allows us to determine exactly how Scheme executes this expression.
a. Write logging versions of all of the procedures used above: /, *, +, -, sqrt, and expt.
Hint: You may need to write multiple versions of multiply_* that take different numbers of parameters. For example, multiply3_* could multiply three values, while multiply2_* would multiply just two values.
b. Write a new version of quadratic that uses these logging procedures. You do not need to document these logging procedures.
c. Using your new annotated version of quadratic, report the order in which scheme evaluates all the sub-expressions in quadratic.
We will post answers to questions of general interest here while the exam is in progress. Please check here before emailing questions!
2.0 instead of just 2. Is that okay?num-largest procedure that will return inexact numbers. You will not lose points if your procedure works this way.student just before turning in your exam.Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. We usually limit such extra credit to five points. However, if we make an astoundingly large number of errors, then we will provide more extra credit. (And no, we don’t count errors in the errata section or the question and answer sections.)
irgb-boost-dominant, irgb-granular-a, and quadratic_*. (GM, +1 point)irgb-granular-b. (AM, +1/2 point)max_boosted and max_value_boosted. (TP, +1/2 point)Some of the problems on this exam are based on (and at times copied from) problems on previous exams for the course. Those exams were written by Charlie Curtsinger, Janet Davis, Rhys Price Jones, Samuel A. Rebelsky, John David Stone, Henry Walker, and Jerod Weinman. Many were written collaboratively, or were themselves based upon prior examinations, so precise credit is difficult, if not impossible.
Some problems on this exam were inspired by conversations with our students and by correct and incorrect student solutions on a variety of problems. We thank our students for that inspiration. Usually, a combination of questions or discussions inspired a problem, so it is difficult and inappropriate to credit individual students.