Write a function to calculate a person's overtime pay, given the person's pay rate, the number of overtime hours, and the pay differential. For example, if a person works 4 hours overtime, their regular rate is $10 and the pay differential is 1.5 (time-and-a-half), then their overtime pay is $60.Step 0: understand the problem and be able to answer it yourself (without a computer). Why is the above example correct? The overtime pay is 4 hours times the overtime pay rate, which in turn is the base rate times the pay differential. So "overtime-pay-rate", while not explicitly mentioned in the problem, turns out to be an important auxiliary concept, and may deserve its own function. No other auxiliary concepts have turned up, so let's go on.
Let's write a contract, examples, header, and template for each function:
; overtime-pay : num (pay-rate), num (ot-hours), num (differential) => num ; Ex: (overtime-pay 10.00 4 1.5) => 60.00 ; Ex: (overtime-pay 12.00 0 1.5) => 0.00 ; Ex: (overtime-pay 12.00 6 1.0) => 72.00 (define (overtime-pay pay-rate ot-hours differential) ( ... pay-rate ... ot-hours ... differential ... )) ; overtime-pay-rate : num (pay-rate), num (differential) => num ; Ex: (overtime-pay-rate 10.00 1.5) => 15.00 ; Ex: (overtime-pay-rate 12.00 1.5) => 18.00 ; Ex: (overtime-pay-rate 12.00 1.0) => 12.00 (define (overtime-pay-rate pay-rate differential) ( ... pay-rate ... differential ... ))
Now we have to come up with bodies for these functions. In keeping with the principle of "top-down design, bottom-up coding and testing", we'll start with the one that doesn't depend on anything else: overtime-pay-rate.
(define (overtime-pay-rate pay-rate differential) (* pay-rate differential))We test this on its examples, confirm that it works in every case, and declare it finished.
Next we'll write the body for the overtime-pay function. Overtime pay is the number of overtime hours times the overtime pay rate, which we've just written a function to compute. So...
(define (overtime-pay pay-rate ot-hours differential) (* ot-hours (overtime-pay-rate pay-rate differential)))We test this on its examples, confirm that it works in every case, and declare it finished.
Then calculate the person's gross pay, given their pay rate, the number of regular hours, the number of overtime hours worked and the pay differential: for this same example, if the person also worked 40 regular hours, the gross pay would be $400 + $60 = $460.In thinking about how to do this ourselves, we realize that gross pay is the sum of regular pay and overtime pay. We already have a function to compute overtime pay, but we may still need one for regular pay, in addition to the gross pay function we were told to write.
Let's write a contract, examples, header, and template for each function:
; gross-pay : num (pay-rate), num (regular-hours), num (ot-hours), num (differential) => num ; Ex: (gross-pay 10.00 40 4 1.5) => 460.00 ; Ex: (gross-pay 12.00 35 5 2.0) => 540.00 (define (gross-pay pay-rate regular-hours ot-hours differential) ( ... pay-rate ... regular-hours ... ot-hours ... differential ... )) ; regular-pay : num (pay-rate), num (regular-hours) => num ; Ex: (regular-pay 10.00 40) => 400.00 ; Ex: (regular-pay 12.00 35) => 420.00 (define (regular-pay pay-rate regular-hours) ( ... pay-rate ... regular-hours ...))Note that regular-pay doesn't depend on overtime hours or the overtime differential, so we won't bother giving it that information.
Again starting with the function that doesn't depend on any others, we write the body
(define (regular-pay pay-rate regular-hours) (* pay-rate regular-hours))Test it. Then write the body of gross-pay, which is simply the sum of the two things we already know how to compute:
(define (gross-pay pay-rate regular-hours ot-hours differential) (+ (regular-pay pay-rate regular-hours) (overtime-pay pay-rate ot-hours differential)))Test it.