title: How can I visualize this recursive implementation of finding the maximum number of a list?
body: <p>I have a recursive function to find the largest number in a list. I wrote the function by following a tip I came across once. The tip suggest that when trying to find the maximum integer in a list of integers is to:    </p>
  
  <ol>
  <li>Cover base cases</li>
  <li>Imagine if there is a another function that gives the max of the tail of the list.</li>
  <li>replace the imaginary function with your function</li>
  </ol>
  
  <p>So the pseudo-code will look like</p>
  
  <pre><code>def max(somelist: List[Int]): Int = {
    // base case 1: empty then error out
    if (somelist.isEmpty) {
     Error out
    }
    // base case 2: if the list has one elements 
    if (somelist.tail.isEmpty) {
      somelist.head
    }
    // Otherwise imagine I have a function that return the max of the tail of the list
    // call it imaginary_max(tailOfList).
    // get the max of the tail and then compare to the head of your list
    // and return the appropriate result.
    // replace imaginary_max with max and it works
  
    else {
      val m = max(somelist.tail);      
      if (somelist.head &gt; m) {
        somelist.head
      }
      else {
        m
      }
    }
  }
  </code></pre>
  
  <p>This works but I am having troubles visualizing what is happening in the background especially when I try to explain it to others. Any help and guidance is appreciated. </p>