title: Using a single function to find min and max numbers in a list without using a driver function?
body: <p>I am currently confused with the idea behind functional programming in general. I currently have a working solution to my problem (That is, finding the min and max of a list, and returning these in a new list) but to do that, my solution essentially requires 3 functions, and this bothers me, because I am sure there is a way to do it with just 1 function in scheme.</p>
  
  <p>So.. my question is, how do I combine the outputs of 2 functions into 1 concise function? (The driver function)</p>
  
  <p>Here is what I have...</p>
  
  <pre><code>(define (findMax lst) ; Find and return maximum number in a list
   (cond [(null? lst) '()]
         [(= (length lst) 1) (list-ref lst 0)]
         [(&gt; (list-ref lst 0) (list-ref lst (- (length lst) 1))) (findMax (drop-right lst 1))]
         [(&lt; (list-ref lst 0) (list-ref lst (- (length lst) 1))) (findMax (cdr lst))]
         (else
          (findMax (cdr lst))
          )
         )
    )
  
  (define (findMin lst) ; Find and return smallest number in a list
   (cond [(null? lst) '()]
         [(= (length lst) 1) (list-ref lst 0)]
         [(&gt; (list-ref lst 0) (list-ref lst (- (length lst) 1))) (findMin (cdr lst))]
         [(&lt; (list-ref lst 0) (list-ref lst (- (length lst) 1))) (findMin (drop-right lst 1))]
         (else
          (findMin (cdr lst))
          )
         )
    )
  </code></pre>
  
  <p>I use a driver function to take both of these functions, and make a new list shown here:</p>
  
  <pre><code>(define (findEnds lst)
    (list (findMin lst) (findMax lst))
    )
  </code></pre>
  
  <p>So essentially, if given a list:</p>
  
  <pre><code>(6 7 8 4 9 2)
  </code></pre>
  
  <p>the output would be:</p>
  
  <pre><code>(2 9)
  </code></pre>
  
  <p>I know there is some way to use lambda possibly to do all of this in 1 function, but I need to be pointed in the right direction. Thanks!</p>