Directions for homework submission.
check-expectform to test your code. You should write enough tests for each function such that all branches of your code are executed. When you press the "Run" button in DrScheme, DrScheme will tell you whether there are any failing tests.
Exercise 1: Design a function that consumes a quantity
(a nonnegative integer), and returns a string representing it in
unary (base 1). In unary, an integer is represented by a number
of symbols (the letter "I", for this exercise) corresponding to
the value of the number. For instance, the number 2 would be
"II" and the number 5 would
"IIIII". To make these numbers readable, add the
number in decimal form after its unary form. Thus, 2 would be
"II (2)" and 5 as
Look up the
string-append in the Help Desk. (if you are
having trouble accessing the Help Desk, you can also get it on
After you are done writing your function, come up with some
tests for the function. You should use
check-expect function to test your code. Press
the "Run" button in DrScheme and make sure all your tests pass
before moving on to the next exercise.
Exercise 2: Design a function that consumes two
quantities (nonnegative integers), and produces a string
illustrating how their unary representations add up (with the
decimal form present as well). For instance, if
4, it should produce the
"II (2) + IIII (4) = IIIIII (6)". You should use
the program you wrote for Exercise 1! If you don't, your code
will be unreadable, and take forever to write.
Don't forget to write tests for your function.
Exercise 3: The Stepper is a DrScheme tool that shows every step a program takes. Click on the Stepper button, the one labeled "Step" with a picture of a foot, to the left of the Run button. Use the Stepper on your programs from Exercise 1 and 2. Figure out what happens in the Stepper when one program calls another program.
Exercise 4: Design a function that calculates late fees on a movie rental. The function consumes the number of days the movie has been rented. Up to 3 days is a regular rental, no fee. For the next week the fee goes up $3 each day. Starting on the 10th day (i.e., a week late) there is a flat $20 fee that never changes. Write tests for your function when you are done. Make sure to try out all the branches in your code.
Exercise 5: Run the Stepper on your program from Exercise 4. Figure out what happens in the Stepper when the program makes a conditional decision.
Exercise 6: Design a function that calculates sales
tax. The function consumes the sale price and the tax
percentage (as a decimal or a fraction) and produces the final
price. For instance, if given
5/100 it should compute 105% of $20,
Exercise 7: Design a function that calculates
conditional sales tax: only prices $100 or more are taxed. The
function consumes a sale price and a tax percentage and produces
the final price. For instance, if given
5/100, it computes $20 (no tax). But if
5/100, it computes $210
Hint: Use your program from Exercise 6.
Did you write tests that covered all the branches of your code for exercises 6 and 7?
Exercise 8: Run the Stepper on your program from Exercise 7. Figure out where the Stepper calls your program from Exercise 6. Try it with values both over and under $100.
Posns are a kind of data provided by DrScheme that represent a position on a plane. As you would expect, a position on a plane is represented using two numbers, one for the value of the x axis, and the other for the y axis.
To create a Posn and use it in your program, you must use
make-posn function. Use the help desk to find
out how to use it. Once you have a Posn, you will likely want to
know what its x and y values are, to pass them to another
function, for example. To do so, use the
posn-y functions, which give you the x and y
values of the Posn, respectively.
Exercise 9: Design a
place-circle that consumes a Posn and
produces a 300-by-300 scene with a red circle of radius 10
at the position represented by the Posn. For instance, if
(make-posn 10 290) it should produce a
scene with a red circle in the lower left corner.
In this part of the lab, we will create an interactive animation.
Add the "universe.ss" teachpack in DrScheme, if it's not there
already. We will use the
We've seen how to use the
on-tick clauses before. Look
them up in Help Desk if you need to remind yourself how they
work. Now we have one more function for reacting to the
mouse pointer. Look up
on-mouse in Help
Desk. Just like
on-tick, we have to give a function
on-mouse. Find out what this
function's inputs and outputs mean, and what a MouseEvent
Exercise 10: Design a
mouse-click to react to clicks. It
consumes a World, two Numbers (x and y coordinates), and a
MouseEvent, as described in
this lab, a World is a Posn (make sure to write this in your
program). When the MouseEvent is
this function produces the x and y coordinates of the mouse
click as a Posn. Any other time, it produces the original
Exercise 11: Put this function and the
place-circle function together to
make an animation. Add the following expression to your
Definitions window, hit Run, and try clicking in the new
window that opens.
(big-bang (make-posn 0 0) (on-draw place-circle) (on-mouse mouse-click))
If you are done early, try playing around with different MouseEvents and see what happens. As a challenge, make it so that the clicking the mouse creates an expanding circle at the cursor's position. (You won't be able to handle multiple circles at once just yet.)