title: Implementing a multiplication-counting function
body: <p>I have written this:</p>
  
  <pre><code>pow :: Integer -&gt; Integer -&gt; Integer
  pow x k = if k == 1
  then x
  else if k `mod` 2 == 0
  then pow (x*x) (k `div` 2)
  else x * pow (x*x) (k `div` 2)
  </code></pre>
  
  <p>Now I want to write a program which has a given <code>k</code> and decides how many multiplications <code>pow x k</code> is going to execute.</p>
  
  <p>My idea was:</p>
  
  <pre><code>costpow :: Integer -&gt; Integer
  costpow 1 = 0
  costpow k = if m == 0 
  then   1 + costpow k
  else  2 + costpow k
  where ( n , m ) = k `divMod` 2
  </code></pre>