Function template for a multi-type output

If the output from the function 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 returns a point parameter is (probably) a cond with two cases:
(define (func-returning-point ...)
   (cond [... some-number]
         [... some-posn]))
Furthermore, if (as in this example) some of the possible output types are themselves complex, the template for those cases can be elaborated according to the compound data type rule:
(define (func-returning-point ...)
   (cond [... some-number]
         [... (make-posn some-number some-number)]))

Last modified: Thu Jan 20 10:12:41 EST 2000
Stephen Bloch / sbloch@adelphi.edu