Function template for a multi-type input parameter

If one (or more) of the input parameters may be of any of several different types, treat this according to the "several possible cases" rule. For example, consider the data definition
; Data definition:
; A point is either
;   a number, indicating a position on the number line, or
;   a posn, indicating a position on the x-y plane.
; A posn is an ordered pair of numbers (x,y)
(define-struct posn (x y))
; make-posn: number number => posn
; posn-x: posn => number
; posn-y: posn => number
; posn?: object => boolean
The template for a function that takes a point parameter is therefore a cond with two cases:
(define (function-for-point the-point)
   (cond [(number? the-point) ...]
         [(posn? the-point) ...]))
Furthermore, if (as in this example) some of the possible types are themselves complex, the template for those cases can be elaborated according to the compound data type rule:
(define (function-for-point the-point)
   (cond [(number? the-point) ...]
         [(posn? the-point) 
          ... (posn-x the-point) ...
          ... (posn-y the-point) ...]))

Last modified: Fri Mar 14 16:29:23 EST 2003
Stephen Bloch / sbloch@adelphi.edu