title: Create a Haskell type class that can define a function that counts the occurrences of two values
body: <p>I am trying to create a type class that defines two functions. One called numMatching. It takes two values and return the count of occurrence the first value in the second value. For example: numMatching 1 1 = 1 since the first value 1 appear once in the second value 1. numMatching 1 0 = 0 since the first value never appear in the second value 0.</p>
  
  <p>For now I have defined the class:</p>
  
  <pre><code>class Counter a where
      numMatching :: a -&gt; a -&gt; Int
      numNonMatching :: a -&gt; a -&gt; Int 
  </code></pre>
  
  <p>and I am trying to create an instance of this class for Int types but I am really
  confused. </p>
  
  <pre><code>instance Counter Int where
      numMatching a b =
          if a == b
              then 1
              else 0
  </code></pre>
  
  <p>I tried to run this function in the ghci by input numMatching 1 1. It should return a value of 1 but I got an error says I need a type annotation to specify what a and b is.</p>
  
  <p>Error: <a href="https://i.stack.imgur.com/PtDMg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PtDMg.png" alt="enter image description here"></a></p>
  
  <p>any help would be appreciated :)</p>