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 (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 and turning it in.
This assignment is to be done in pairs, just like previous homeworks, but with a different partner.
Recall that the posn
structure is already defined in
DrScheme. But if it weren't, we could have defined it as follows:
; A posn consists of two numbers, x and y. (define-struct posn [x y]) ; The define-struct automatically gives us the following functions: ; make-posn : number number => posn ; posn-x : posn => number ; posn-y : posn => number ; posn? : object => booleanTo make sure we're comfortable using these functions, let's write some examples.
(make-posn 3 4) "should be itself, i.e. (make-posn 3 4)" (define here (make-posn 5 12)) (posn-x here) "should be" 5 (posn-y here) "should be" 12 (posn? here) "should be" true (posn? 7) "should be" false
Now that we've defined this data type, we can write a variety of
functions that work with it. As a warm-up to writing real functions,
we'll write template functions
function-for-posn
and function-returning-posn
,
which aren't "real" functions, but patterns that can be copied and
pasted every time you have a real function to write. They
look like this:
(define (function-for-posn the-posn) the-posn ; a posn (posn-x the-posn) ; a number (posn-y the-posn) ; a number ) (define (function-returning-posn whatever) (make-posn some-number another-number))For example, if we wanted to define the
distance-to-0
function, which takes in a posn, we would start by copying and pasting
function-for-posn
and changing the name to get
(define (distance-to-0 the-posn) the-posn ; a posn (posn-x the-posn) ; a number (posn-y the-posn) ; a number )We would then put these ingredients together using the distance formula to get
(define (distance-to-0 the-posn) ; the-posn ; a posn ; (posn-x the-posn) ; a number ; (posn-y the-posn) ; a number (sqrt (+ (sqr (posn-x the-posn)) (sqr (posn-y the-posn)))) )This sequence of steps is described in Design Recipe, version 3.
Do (but don't turn in) some of the exercises in sections 6.3, 6.4, and 6.5, as many as you think you need in order to feel comfortable defining structures. Although you don't need to turn these in, make sure you know how to do them before going on to the next exercises.
function-for-account
and
function-returning-account
analogous to
function-for-posn
and function-returning-posn
above.
Comment them out (I suggest using the DrScheme command "Comment with a
box"), and use them as a starting point in writing the following two functions.
Develop a function compute-interest
which takes in a bank account and returns the amount of interest that
it would earn in a year. For example,
(compute-interest (make-account 800 1.5 5)) "should be" 12
Develop the function deposit
, which
takes an
account and an amount, and returns a new account just like the original one,
but with a changed balance and one more transaction. For example,
(deposit (make-account 800 1.5 5) 200) "should be" (make-account 1000 1.5 6)
Define a structure to represent a candidate in an
election, comprising a name (represented by a string) and a number of votes.
Again, write down the contracts of the automatically-defined
functions and some examples of how to use them.
Then write two function templates
function-for-candidate
and
function-returning-candidate
analogous to
function-for-posn
and function-returning-posn
above.
Comment them out
and use them as a starting point in writing the following two functions.
Develop a function who-won
that takes in three
candidate structures and returns the winning struct (i.e. both the
name and the number of votes). You may assume there are no ties. (For
extra credit, return the string
"tie"
if there is no single winner.)
For example,
(who-won (make-candidate "Anne" 3) (make-candidate "Bertha" 4) (make-candidate "Cyril" 2)) "should be" (make-candidate "Bertha" 4)
Develop a function collect-votes-4
which
takes in five strings -- the name of a candidate, and four votes -- and
returns a struct with the candidate's name and how many votes (s)he got.
For example,
(collect-votes-4 "Bob" "Bob" "Charlie" "Charlie" "Bob") "should be" (make-candidate "Bob" 2)(Hint: this should be very short and simple, re-using previously-defined functions.)
Develop a function 4-votes->winner
that
takes in four strings representing votes, and returns both the name of the
winner and how many votes (s)he got, bundled together in a candidate
structure. As in homework 4, you may assume that the only candidates are
"Anne", "Bob", and "Charlie". (We'll drop that assumption in
homework 8.) For example,
(4-votes->winner "Anne" "Charlie" "Charlie" "Bob") "should be" (make-candidate "Charlie" 2)(Hint: re-use previously-defined functions.)
Error log: /15
account struct
|
Definition: /5 | Contracts: /5 | Examples: /5 | Templates: /10 |
compute-interest |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
deposit |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
candidate struct
|
Definition: /5 | Contracts: /5 | Examples: /5 | Templates: /10 |
who-won |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
collect-votes-4 |
Contract: /5 | Examples: /5 | Definition: /10 | Results: /5 |
4-votes->winner |
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 |