Re-read the adages page,
and choose one (or several closely-related) adages, or one
longer article, that mean more to you than they did in September.
Write an essay of one or more well-structured
paragraphs: what does it really mean in practice?
Do you agree or disagree?
Support your claims with specific examples from your own
programming experience.
As usual, you need to turn in your source code (the contents of a definitions window), your test runs (the contents of an interactions window after you've executed the definitions), and your error log. You may keep the error log in a text file and e-mail it in, or you may instead use the PSP on-line forms.
As usual, be sure to follow the design recipe for every function.
You may use either nested-cons
notation or list
notation, whichever you are more comfortable with.
For the first few problems, you'll need to set DrScheme to at least the "Intermediate Student" language level. For the last few, you'll need "Advanced Student".
Choose a problem you've previously turned in that
required one or more auxiliary functions. Re-write
this problem with the auxiliary functions hidden inside a
local
block.
Review the function largest
, discussed
in class on Dec. 1.
Develop a similar function
smallest
which takes a non-empty list of numbers and returns the
smallest. To test whether it's reasonably efficient, test it on a list of
twenty or more numbers in decreasing order.
Develop a similar function
leftmost
which takes a non-empty list of posn
s
and returns the one with the smallest x-coordinate.
Develop a generalized function
champion
which takes a non-empty list of X's (where X is
any data type) and a function (let's call it beats?
)
with the contract "X X -> boolean", and returns the element of
the list that "beats" all the others. For example,
(champion (list 5 2 4 6 3) >) "should be 6"
(champion (list 5 2 4 6 3) <) "should be 2"
Re-write smallest
using champion
.
Your new definition should fit on one or two reasonably short lines.
Re-write leftmost
using
champion
. Your new definition should fit on at most four
reasonably short lines.
Re-write the winner
function from homework 8 using champion
. (If you didn't
finish the winner
function before, here's your second chance.)
Your new definition should fit on at most four reasonably short lines.
Note: Recall that winner
returned the
name of the winner, not the whole winner. Use local
and champion
to get the whole winner, and then extract the
winner's name.
To test whether your winner
function is efficient enough,
test it on a long list of candidates in increasing order of votes, e.g.
(list (make-candidate 'ann 1) (make-candidate 'bob 2)
(make-candidate 'charlie 3) (make-candidate 'dolores 4)
(make-candidate 'eddie 5) (make-candidate 'frank 6) (make-candidate 'george 7)
(make-candidate 'hilda 8) (make-candidate 'ian 9) (make-candidate 'jeff 10)
(make-candidate 'karen 11) (make-candidate 'lee 12) (make-candidate 'mary 13)
(make-candidate 'ned 14) (make-candidate 'omar 15) (make-candidate 'phil 16)
(make-candidate 'quint 17) (make-candidate 'ron 18) (make-candidate 'sam 19)
(make-candidate 'tess 20) (make-candidate 'uma 21) (make-candidate 'velma 22)
(make-candidate 'wanda 23) (make-candidate 'xena 24)
(make-candidate 'yuri 25) (make-candidate 'zelda 26))
It should take less than a second. (If you wrote winner
for
homework 7, test that version on the same test case and see what happens.)
Set DrScheme to the "Advanced Student" language level. You won't know how to solve this problem until Dec. 8.
Develop a function namedcount-down
that
takes in a natural number and prints (using display
) the
numbers from that number down to zero, each on a separate line, ending
with "blastoff!" in place of the number 0. For example, You won't know how to solve this problem until Dec. 8.
Develop a function named
give-raise!
which takes an employee
struct
(from homework 8) and
a number (e.g. 0.10 for a 10% raise), and modifies the employee
to earn that much more than before. The function returns nothing. For example,
(define joe (make-employee 'joe 50000)) (give-raise! joe 0.10) "should be nothing" joe "should be (make-employee 'joe 55000)"
Develop a function named give-raises!
which takes a list of employee
s and a number, and
modifies each of them with that percentage raise. Don't worry about the
return value, but look for opportunities to re-use previously-written
functions (which may require defining a local
function).
For example,
(define joe (make-employee 'joe 50000)) (define mary (make-employee 'mary 60000)) (give-raises! (list joe mary) 0.10) "may return something, but I don't care" joe "should be (make-employee 'joe 55000)" mary "should be (make-employee 'mary 66000)"
Extra credit: Develop a function named
ask-and-give-raises!
which takes a list of employees
.
For each employee, your function will print a message on the screen with
the person's name, asking how much raise (e.g. 0.10 for a 10% raise)
that person should get;
it will then wait for the user to type a number, modify that person's
salary, and go on to the next employee. Ideally, the function should
return nothing, but don't worry about that too much.
As usual, look for opportunities
to re-use previously-written functions.
For example,
Essay: /300
Error log: /15
rewriting w/local |
Definition: /20 | Results: /5 | ||
smallest |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
leftmost |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
champion |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
re-write smallest |
Definition: /10 | Results: /5 | ||
re-write leftmost |
Definition: /10 | Results: /5 | ||
re-write winner |
/5 | /5 | Definition: /10 | Results: /5 |
count-down |
/5 | /5 | Definition: /10 | Results: /5 |
give-raise! |
/5 | /5 | Definition: /10 | Results: /5 |
give-raises! |
/5 | /5 | Definition: /10 | Results: /5 |
ask-and-give-raises! (extra credit) |
/5 | /5 | Definition: /10 | Results: /5 |
Following directions | /20 |
Writing contracts from word problems | /20 |
Choosing examples | /20 |
Choosing names | /20 |
Coding | /20 |
Code re-use and choice of auxiliaries | /20 |