For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and send me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Execute button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and send me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
For this and the next homework assignment, I recommend using
list
notation rather than nested-cons
notation for your examples.
Develop a function convert-reversed
that
takes in a list of numbers representing the digits of a decimal number
(ones place first, then tens place, and so on) and returns the
number. This is similar to the convert-3-digits-reversed
function from homework 1, but instead of taking in three digits, it now
takes one argument which is an arbitrary-length list
of digits. For example,
(convert-reversed (list 3 0 5 4 1)) "should be" 14503Note: The reason the digits are in reverse order is that it's actually easier to write the function that way. For extra credit, write a
convert
function that does the same
thing, with the digits given in the usual order.
Develop a function plot-points
that
takes in a list of posn
s and draws a little dot on the
screen at each location. The function should return true
,
like most of the graphics functions we've seen.
(define-struct employee [name id salary])(Recall that I used this definition to write
total-salary
in class. Review this function and its examples before going on.)
We'll be working with lists of employees; call this the
"employee-list" data type.
With the aid of the templates template-for-employee-list
and template-returning-employee-list
,
develop the following functions:
count-over-100K
, which takes a list of employees and
tells how many of them have a salary over $100,000.give-10%-raises
, which takes a list of employees and
returns a list of employees with the same names and ID's, but each
making 10% more than before.fire-over-100K
, which takes a list of
employees and returns a new list of employees, leaving out the ones who
earned over $100,000.give-raise
, which works on a single
employee. And you'll probably need to use the
"template with values" strategy discussed in class.
Recall the candidate
structure you defined in the
previous homework.
We'll be working with lists of candidates; call this the
"candidate-list" data type.
Write several examples of candidate lists.
Write
template-for-candidate-list
and
template-returning-candidate-list
and comment them out.
Then, with the aid of these templates, develop the following functions:
add-vote-for
, which takes in a string (the name of a
candidate) and a list of candidate
structures, and
returns a new list of candidate
structures with one more vote
recorded for that candidate. Note: the named candidate
may not already be in the list at all; handle this case by
creating a candidate
structure with that name and one
vote.
Hint: You may want to write an auxiliary function that
works on a single candidate.
count-votes
, which takes in a list of strings
representing votes (e.g.
(list "anne" "bob" "anne" "charlie" "charlie" "anne")and returns a list of
candidate
structures, each with the correct number of
votes. For example, the above list should produce
(list (make-candidate "anne" 3) (make-candidate "bob" 1) (make-candidate "charlie" 2))Note: I don't care about the order of the candidates, only that each one has the right number of votes, so if your function returned
(list (make-candidate "bob" 1) (make-candidate "charlie" 2) (make-candidate "anne" 3) )
,
that would be OK.
winner
, which takes in a
list of candidate
structures and
returns the name of the winner.
You may assume that the list is non-empty.
Hint: You'll need at least one auxiliary function. I see
two approaches: one uses an auxiliary function that looks up a given name
in a list of candidates, and the other uses an auxiliary function just like
winner
except that it returns a whole candidate
rather than just the name. You may use either of these approaches, or any
other approach as long as it works.
Extra credit: return the string "tie" if there is no single winner.
election
, which takes in
a list of strings (representing the votes cast by individual voters) and
returns a string, the winner of the election. You may assume that there
is at least one vote, and that there are
no ties for first place. (If you did the extra-credit version of
"winner", you can drop the latter assumption.)
Develop a function named
"table-of-squares
" which takes in a natural number
n
and produces a list of posn
s, associating each of
the numbers 1...n
with its square. For example,
(table-of-squares 5)
should return
(list (make-posn 5 25) (make-posn 4 16) (make-posn 3 9) (make-posn 2 4) (make-posn 1 1))Note: I've put these in descending order because it's easier to write the function that way. For extra credit, produce the table in increasing order.
What would happen if you called
plot-points
on the result of calling
table-of-squares
? Write down what you think will happen,
then try it.
Extra credit: Recall the oranges
function
I wrote in class, which takes a natural number and produces an image of the
specified number of oranges, side by side.
Develop a function orange-stack
which takes a natural number and produces an image of a triangular stack
of oranges of the specified height.
Do exercise 11.4.7, a function which tests whether a number is prime.
Error log: /60
convert-reversed |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
convert extra credit |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
plot-points |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
count-over-100K |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
give-10%-raises |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
fire-over-100K |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
add-vote-for |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
count-votes |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
winner |
Contract: /10 | Examples: /10 | Definition: /20 | Results: /10 |
winner extra credit |
Definition: /5 | Results: /5 | ||
election |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
table-of-squares |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
table-of-squares composed with plot-points |
Prediction: /5 | |||
orange-stack extra credit |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
11.4.7: is-not-divisible-by<=i |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
11.4.7: prime? |
Contract: /5 | Examples: /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 |