title: Haskell (n+1) in pattern matching
body: <p>I was doing the <a href="http://haskell.org/haskellwiki/99_questions/" rel="noreferrer">99 Problems in Haskell</a> when I encountered a <a href="http://haskell.org/haskellwiki/99_questions/Solutions/19" rel="noreferrer">solution</a> to <a href="http://haskell.org/haskellwiki/99_questions/11_to_20#Problem_19" rel="noreferrer">Problem 19</a> that I did not fully understand.</p>
  
  <p>The task is to write a rotate function that works like this</p>
  
  <pre><code>*Main&gt; rotate ['a','b','c','d','e','f','g','h'] 3
  "defghabc"
  
  *Main&gt; rotate ['a','b','c','d','e','f','g','h'] (-2)
  "ghabcdef"
  </code></pre>
  
  <p>One provided solution is</p>
  
  <pre><code>rotate [] _ = []
  rotate l 0 = l
  rotate (x:xs) (n+1) = rotate (xs ++ [x]) n
  rotate l n = rotate l (length l + n)
  </code></pre>
  
  <p>I don't understand how the pattern matching can ever reach the fourth line. It seems to have to do with the <code>(n+1)</code> so that when <code>n</code> is negative the third line does not match and therefore the fourth is taken. If that is the case why does the notation <code>(n+1)</code> work that way resp. isn't that arbitrary or is that a convention (in mathematics?) that I'm not aware of? </p>
  
  <p>Because the way I understand it is that rotate is called recursively in the third line with the argument <code>n</code> reduced by one. So I would think that</p>
  
  <pre><code>rotate [] _ = []
  rotate l 0 = l
  rotate (x:xs) n = rotate (xs ++ [x]) (n-1)
  rotate l n = rotate l (length l + n)
  </code></pre>
  
  <p>is equivalent. However, it is not. This definition gives the following warning</p>
  
  <pre><code>Warning: Pattern match(es) are overlapped
           In the definition of `rotate': rotate l n = ...
  </code></pre>
  
  <p>whereas the former definition compiles just fine.</p>