Function template for list parameters of unknown length

If one of the input parameters is a list of unknown length, combine the "several possible cases" template with the "compound data type" template.

Data definition:
a list-of-unknown-length is either
  1. empty , or
  2. (cons something some-other-list-of-unknown-length)
so the body will be a cond with two cases:
(define (template-for-list the-list)
   (cond [(empty? the-list) ...]
         [(cons? the-list) ...]))
Furthermore, if the list is a cons, you know that it has a first and a rest, so by the "compound data type" rule you can expand the template to
(define (template-for-list the-list)
   (cond [(empty? the-list) ...]
         [(cons? the-list) 
          ... (first the-list) ...
          ... (rest the-list) ...]))
What you can do with "(first the-list)" and "(rest the-list)" depends on their types. Since rest always returns a list of the same type as it was given (only one item shorter), the most likely thing to do with "(rest the-list)" is to apply the same function to it:
(define (template-for-list the-list)
   (cond [(empty? the-list) ...]
         [(cons? the-list) 
          ... (first the-list) ...
          ... (template-for-list (rest the-list)) ...]))

Last modified: Thu Nov 4 16:39:55 EST 1999
Stephen Bloch / sbloch@adelphi.edu