For all the programming assignments, be sure to follow the design recipe. For functions, 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.
If you need help with the functions that require images or animations,
please see the
documentation for the tiles-world
teachpack.
Feel free to use either list
or cons
notation
for your examples, whichever you prefer.
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 at the beginning of the term.
Write an essay of one or more well-structured
paragraphs: what does it really mean in practice?
Do you agree or disagree?
Is it relevant or useful in your own field or career?
Support your claims with specific examples from your own
programming experience.
Develop a function all-over-10?
that
takes in a list of numbers and tells whether all of them are
greater than 10. Hint: the base case may not be what
you expect!
Develop a function named convert-reversed
which takes in a list of numbers representing digits in a (possibly long) number,
in reverse order (this makes it easier! Honest!)
and returns a single number. You may assume that each of the numbers in the input
list is an integer, at least 0 and no more than 9. For example,
(convert-reversed (list 5 2 0 7)) "should be" 7025
Develop a function recall
that
takes a string and a list of strings and returns the same list with all
occurrences of the given string removed.
Example:
(recall "bat" (list "doll" "bat" "bat" "nintendo" "bat" "ball")) "should be" (list "doll" "nintendo" "ball")
Develop a function remove-first
that
takes a string and a list of strings and returns the same list with
the first occurrence (if there are any) of the given string
removed.
Example:
(remove-first "bat" (list "doll" "bat" "bat" "nintendo" "bat" "ball")) "should be" (list "doll" "bat" "nintendo" "bat" "ball")
Develop a function replace
that takes two strings (referred to as old
and new
)
and a list of strings,
and returns a list of strings with old
replaced
throughout by new
.
Example:
(replace "bat" "ball" (list "doll" "bat" "bat" "nintendo" "bat" "ball")) "should be" (list "doll" "ball" "ball" "nintendo" "ball" "ball")
Copy the definition of the emp
structure, and the
templates template-for-emp
and template-for-emps
from
June 14 examples.
Using these templates as appropriate, 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.fire-highly-paid
, which takes a number and a list of
employees and returns a new list of employees, leaving out the ones who
earned over the specified amount. (Note that this is a generalization
of fire-over-100K
.)sort-by-salary
, which takes a list of employees and
returns a list of the same employees sorted by salary, from lowest-paid
to highest-paid. (This one requires Chapter 12.)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.
Recall the
random-posn
function from homework 2.
Develop a function named
"random-posns
" which takes in a natural number
n
and two other numbers
max-x
and max-y
, and produces a list of that many
posn
s, each randomly chosen as in random-posn
,
above.
Recall the candidate
structure from homework 3.
Write a function template for functions that take in a list of candidate structures.
Develop a function named
add-vote-for
which takes in a string (the name of a candidate) and a list of
candidate structures, and returns a list of candidate structures
in which that candidate has one more vote (and all the others are
unchanged). You may assume that no name appears more than once in
the list of candidate structures.
Note: it's possible that the name doesn't appear in the list
at all (e.g. it's a write-in vote, or something like that);
if so, the resulting list should have 1 vote for that candidate.
Develop a function named tally-votes
which takes in a list of strings (Voter 1's favorite candidate,
Voter 2's favorite candidate, etc.) and produces a list of candidate structures
in which each candidate name appears once, with how many votes were cast
for that candidate.
Develop a function named sort-candidates
which takes in a list of candidates and sorts them in order by votes, so
that the one with the most votes is first and the one with the fewest votes
is last. (Requires Chapter 12.)
Let's keep building on that animation of the rocketship. First, we'll keep our rocketships from flying off the edges of the window. Then we'll animate several rocketships, not just one. (It would be really cool to get the rocketships to avoid crashing into one another, too, but that's more complicated.)
Modify the handle-tick
function from
homework 3 so that if a rocketship goes off the window to the right, it
re-appears on the left, and vice versa; if it goes off the window to the
bottom, it re-appears at the top, and vice versa. Its velocity should
not be affected: if it was moving right, it should still be moving right.
Extra credit: modify the handle-tick
function so that instead of teleporting from right to left edges, it uses
its engine to avoid hitting the edge of the window. If it's within
20 pixels of the right edge of the window, it accelerates to the left as
though you had hit the left-arrow key; if it's within 20 pixels of the top
edge , it accelerates down as though you had hit the down-arrow
key; and so on. Note that if it's going really fast, it might still hit
(and go through) the wall; don't worry about this.
Rename the handle-tick
function
(regardless of whether you did the extra credit) to update-rocket
.
Develop a new animation in which the world is a list
of rockets. At each clock tick, apply update-rocket
to all
of them, so each one moves according to its own current velocity (and wraps
around the edges, or avoids walls, individually). When you press an
arrow key, all of them accelerate in the specified direction. And
when you press the home key (recognizable in handle-key
by the
symbol 'home
), it adds a new rocket at the center of the window,
initially not moving.
(Hint:
This will require writing a new show-rockets
function,
modifying handle-key
, rewriting handle-tick
,
and changing the call to big-bang
so it starts with a list.
Several of these tasks will be much easier if you use do-to-each
,
which requires the Intermediate Student language.)
Error log: /20
Essay: /50
Function or struct name | Data def'n | Define-struct | Contracts for struct functions |
Template | Contract | Examples | Definition | Test runs |
---|---|---|---|---|---|---|---|---|
all-over-10? function |
/5 | /5 | /10 | /5 | ||||
convert-reversed function |
/5 | /5 | /10 | /5 | ||||
recall function |
/5 | /5 | /10 | /5 | ||||
remove-first function |
/5 | /5 | /10 | /5 | ||||
replace function |
/5 | /5 | /10 | /5 | ||||
employee-list type | given here | |||||||
count-over-100K function |
/5 | /5 | /10 | /5 | ||||
fire-highly-paid function |
/5 | /5 | /10 | /5 | ||||
sort-by-salary function |
/10 | /10 | /20 | /10 | ||||
natural number type | given in class | |||||||
table-of-squares function |
/5 | /5 | /10 | /5 | ||||
increasing-table-of-squares function |
/5 | /5 | /10 | /5 | ||||
random-posns function |
/5 | /5 | /10 | /5 | ||||
candidate-list type | /5 | /15 | ||||||
add-vote-for function |
/5 | /5 | /10 | /5 | ||||
tally-votes function |
/5 | /5 | /10 | /5 | ||||
sort-candidates function |
/5 | /5 | /10 | /5 | ||||
rocket-list type | /5 | /15 | ||||||
wraparound movement | /5 | /5 | /10 | /5 | ||||
avoiding walls by acceleration | /10 | /10 | /20 | /10 | ||||
animation functions | /15 | /15 | /30 | /15 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |