Exercise 1: Design a function that consumes a quantity (number)
and a unit (symbol) representing a measurement and produces a string
describing the measurement. For instance, if given 3
and
'inches
it should produce "3 inches"
.
Hint: Look up number->string
and symbol->string
in Help Desk.
Exercise 2: Design a function that consumes two quantities
(numbers) and a unit (symbol) and produces a string describing both
measurements and their sum, all in the given unit. For instance, if
given 3
, 4
, and 'pounds
it should
produce "3 pounds + 4 pounds = 7 pounds"
.
Hint: Use the program you wrote for Exercise 1.
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 there is a flat $20 fee that never changes.
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 fraction of
the original price) and produces the final price. For instance, if
given 20
and 5/100
it should compute 105% of
$20, or 21
.
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 20
and 5/100
, it computes $20 (no tax).
But if given 200
and 5/100
, it computes $210 (5%
tax).
Hint: Use your program from Exercise 6.
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.
Structures are collections of data that are treated as one larger
datum. DrScheme offers a mechanism to define structures using the
define-struct
statement.
When we define a new structure, in the background, DrScheme provides a
series of functions that help us create and manipulate the new
structure and its contents.
Look up define-struct
in the Help Desk to check its syntax
and the functions it defines.
In addition, DrScheme contains some built-in structures like Posns:
;; A Posn is (make-posn Number Number) (define-struct posn (x y))A Posn is a structure defining a position with x and y coordinates. DrScheme provides Posns with the definition above. Write down the Posn functions provided by this definition. How many should there be? Remember to include constructors and selectors.
Exercise 9:
Design a function place-circle
that consumes a Posn and
produces a 300-by-300 scene with a red circle of radius at the
position represented by the Posn. For instance, if
given (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 "world.ss" teachpack in DrScheme. Let's check the built-in functions we will use:
big-bang : Number Number Number World -> true on-redraw : (World -> Image) -> true on-tick-event : (World -> World) -> true on-mouse-event : (World Number Number MouseEvent -> World) -> trueWe've seen the first three animation functions 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-event
in Help Desk. Just
like on-redraw
and on-tick-event
, we have to
give a function to on-mouse-event
. Find out what this
function's inputs and outputs mean, and what a MouseEvent is.
Exercise 10: Design a function mouse-click
to react to clicks. It
consumes a World, two Numbers (x and y coordinates), and a MouseEvent,
as described in on-mouse-event
. For our purposes, a
World is a Posn (make sure to write this in your program). When the
MouseEvent is 'button-down
, this function produces the x
and y coordinates of the mouse click as a Posn. Any other time, it
produces the original World unchanged.
Exercise 11: Put these two functions together to make an animation. Add the following three lines to your Definitions window, hit Run, and try clicking in the new window that opens.
(big-bang 300 300 .1 (make-posn 0 0)) (on-redraw place-circle) (on-mouse-event mouse-click)