title: Folding on Type without Monoid Instance
body: <p>I'm working on this <a href="http://www.manning.com/bjarnason/" rel="nofollow">Functional Programming in Scala</a> exercise:</p>
  
  <pre><code>// But what if our list has an element type that doesn't have a Monoid instance?
  // Well, we can always map over the list to turn it into a type that does.
  </code></pre>
  
  <p>As I understand this exercise, it means that, if we have a Monoid of type <code>B</code>, but our input List is of type <code>A</code>, then we need to convert the <code>List[A]</code> to <code>List[B]</code>, and then call <code>foldLeft</code>.</p>
  
  <pre><code>def foldMap[A, B](as: List[A], m: Monoid[B])(f: A =&gt; B): B = {
      val bs = as.map(f)
      bs.foldLeft(m.zero)((s, i) =&gt; m.op(s, i))
  }
  </code></pre>
  
  <p>Does this understanding and code look right?</p>