The toll problem

At a particular toll bridge, Class 1 vehicles are charged 25 cents, Class 2 vehicles are charged 50 cents, and Class 3 vehicles are charged 75 cents. Write a function that takes a class number (1, 2, or 3) as input, and returns the toll amount.
In reading the problem, we realize that there are several cases to be considered. The data analysis step of the design recipe calls for listing the cases: in this case, the input is a number which will be either 1, 2, or 3. For now, let's just hope that it is one of these cases, and not worry (yet) about what to do if we are given an illegal input.

Now we'll write a contract, examples, header, and template for the function:

; toll : num (class -- should be 1, 2, or 3) => num (toll)
; Ex: (toll 1) => 0.25
; Ex: (toll 2) => 0.50
; Ex: (toll 3) => 0.75
; Ex: (toll 0) => error; we sha'n't worry about this case yet.
(define (toll class)
	(cond [ question answer ]
	      [ question answer ]
	      [ question answer ]))
Note that we listed an example for each case. Note also that we know there will be three cases to the cond because the data analysis showed us three cases.

Next we fill in the questions:

(define (toll class)
	(cond [(= class 1) answer ]
	      [(= class 2) answer ]
	      [(= class 3) answer ]))
and then the answers:
(define (toll class)
	(cond [(= class 1) 0.25]
	      [(= class 2) 0.50]
	      [(= class 3) 0.75]))
We now test this on each example:
(toll 1)
0.25
(toll 2)
0.50
(toll 3)
0.75

Looks good!


Last modified: Mon Sep 20 15:41:33 EDT 1999
Stephen Bloch / sbloch@adelphi.edu