title: *** Exception: Prelude.head: empty list in Haskell
body: <p>I am trying to write a function that does this : 
  func [3,3,5,7] 1 = [(3,2),(5,1),(7,1)]
  It makes tuple of unique elements in a list and gives the occurence of the elements. I wrote this : </p>
  
  <pre><code>func [] n = []
  func (x:xs) n = if x == head xs then func (xs) (n + 1) else (x, n) : func (xs) 1
  </code></pre>
  
  <p>I get this exception :</p>
  
  <blockquote>
    <p><em>*</em> Exception: Prelude.head: empty list</p>
  </blockquote>
  
  <p>In order to fix this I write this : </p>
  
  <pre><code>func [] n = []
  func [x] n = (x,n)
  func (x:xs) n = if x == head xs then func (xs) (n + 1) else (x, n) : func (xs) 1
  </code></pre>
  
  <p>But now I get this error :</p>
  
  <blockquote>
    <p>Couldn't match expected type <code>[a0]' with actual type</code>(t0, t1)' In the
    expression: (x, n) In an equation for func: func [x] n = (x, n)</p>
  </blockquote>
  
  <p>How can I fix this? </p>