title: Using list elements and indices together
body: <p>I've always found it awkward to have a function or expression that requires use of the values, as well as indices, of a list (or array, applies just the same) in Haskell.</p>
  
  <p>I wrote <code>validQueens</code> below while experimenting with the N-queens problem <a href="https://stackoverflow.com/questions/6470412/extent-of-ghcs-optimization"> here </a>...</p>
  
  <pre><code>validQueens x = 
       and [abs (x!!i - x!!j) /= j-i | i&lt;-[0..length x - 2], j&lt;-[i+1..length x - 1]]
  </code></pre>
  
  <p>I didn't care for the use of indexing, all the plus and minuses, etc.  It feels sloppy.  I came up with the following:</p>
  
  <pre><code>enumerate x = zip [0..length x - 1] x
  
  validQueens' :: [Int] -&gt; Bool
  validQueens' x = and [abs (snd j - snd i) /= fst j - fst i | i&lt;-l, j&lt;-l, fst j &gt; fst i]
                     where l = enumerate x 
  </code></pre>
  
  <p>being inspired by Python's <code>enumerate</code> (not that borrowing imperative concepts is necessarily a great idea).  Seems better in concept, but <code>snd</code> and <code>fst</code> all over the place kinda sucks.  It's also, at least at first glance, costlier both in time and space.  I'm not sure whether or not I like it any better.</p>
  
  <p>So in short, I am not really satisfied with either</p>
  
  <ol>
  <li>Iterating thru by index bounded by lengths, or even worse, off-by-ones and twos</li>
  <li>Index-element tuples</li>
  </ol>
  
  <p>Has anyone found a pattern they find more elegant than either of the above?  If not, is there any compelling reason one of the above methods is superior?</p>