title: How to implement a custom tail recursive map for lists in Scala
body: <p>I want to implement a function called map that receives a list and a function and the yields the result of applying the function to each element of the input list.
  So I have this so far:</p>
  
  <pre><code>def map(list: List[Int], function: (Int) =&gt; Int): List[Int] = {
    def loop(list: List[Int], acc: List[Int]): List[Int] = {
      list match {
        case Nil =&gt; acc
        case head :: tail =&gt; loop(list.tail, function(head) :: acc)
      }
    }
    loop(list.reverse, Nil)
  }
  </code></pre>
  
  <p>This code works and gives me the expected result, but I cannot help thinking there is a more elegant and efficient way that doesn't involve using <code>reverse</code> or another list method that isn't <code>head</code> or <code>tail</code>.</p>