title: Get index of next smallest element in the list in Haskell
body: <p>I m a newbie to Haskell. I am pretty good with Imperative languages but not with functional. Haskell is my first as a functional language. </p>
  
  <p>I am trying to figure out, how to get the index of the smallest element in the list where the minimum element is defined by me. </p>
  
  <p>Let me explain by examples.</p>
  
  <p>For example :</p>
  
  <p>Function signature
      minList :: x -> [x]</p>
  
  <pre><code>let x = 2
  let list = [2,3,5,4,6,5,2,1,7,9,2] 
  
  minList x list --output 1 &lt;- is index
  </code></pre>
  
  <p>This should return 1. Because the at list[1] is 3. It returns 1 because 3 is the smallest element after x (=2).</p>
  
  <pre><code>let x = 1
  let list = [3,5,4,6,5,2,1,7,9,2] 
  minList x list -- output 9 &lt;- is index
  </code></pre>
  
  <p>It should return 9 because at list[9] is 2 and 2 is the smallest element after 1. x = 1 which is defined by me.</p>
  
  <p>What I have tried so far.</p>
  
  <pre><code>minListIndex :: (Ord a, Num  a) =&gt; a -&gt; [a] -&gt; a
  minListIndex x [] = 0
  minListIndex x (y:ys) 
              | x &gt; y =  length ys
              | otherwise = m
              where m = minListIndex x ys
  </code></pre>
  
  <p>When I load the file I get this error</p>
  
  <pre><code>• Couldn't match expected type ‘a’ with actual type ‘Int’
        ‘a’ is a rigid type variable bound by
          the type signature for:
            minListIndex :: forall a. (Ord a, Num a) =&gt; a -&gt; [a] -&gt; a
          at myFile.hs:36:17
      • In the expression: 1 + length ys
        In an equation for ‘minListIndex’:
            minListIndex x (y : ys)
              | x &gt; y = 1 + length ys
              | otherwise = 1 + m
              where
                  m = minListIndex x ys
      • Relevant bindings include
          m :: a (bound at myFile.hs:41:19)
          ys :: [a] (bound at myFile.hs:38:19)
          y :: a (bound at myFile.hs:38:17)
          x :: a (bound at myFile.hs:38:14)
          minListIndex :: a -&gt; [a] -&gt; a (bound at myFile.hs:37:1)
  </code></pre>
  
  <p>When I modify the function like this</p>
  
  <pre><code>minListIndex :: (Ord a, Num  a) =&gt; a -&gt; [a] -&gt; a
  minListIndex x [] = 0
  minListIndex x (y:ys) 
              | x &gt; y =  2 -- &lt;- modified...
              | otherwise = 3 -- &lt;- modifiedd
              where m = minListIndex x ys
  </code></pre>
  
  <p>I load the file again then it compiles and runs but ofc the output is not desired.</p>
  
  <p>What is the problem with </p>
  
  <pre><code>| x &gt; y =  length ys
  | otherwise = m
  </code></pre>
  
  <p>?</p>
  
  <p>In short: Basically, I want to find the index of the smallest element but higher than the x which is defined by me in parameter/function signature.</p>
  
  <p>Thanks for the help in advance!</p>