title: Haskell Runtime Error: error: Prelude.(!!): index too large
body: <p>I have a long and slightly wacky function in Haskell.</p>
  
  <pre><code>(#==#) :: String -&gt; String -&gt; Bool
  str1 #==# str2 = (sum[ 1 | index &lt;- [0..(max (length str1) (length str2))], (str1!!index == str2!!index || str1!!index == '$')] == (max (length str1) (length str2)))
  </code></pre>
  
  <p><strong>In short, this function checks if two string are either identical, and considers them identical if they have one or more '$'</strong>
  [Long Version: To save you time from deciphering it, it take two strings, an index variable inside the list comprehension goes from 0 until the length of the longest String. Then the element of each String at the current index is compared to eachother or the dollar sign. Both are OK. If they are one of those, a 1 is added to the new list, and if the sum of this new list is equal to the length, then the word is a match.</p>
  
  <p>When I try running this though, I get a peculiar error:</p>
  
  <pre><code>*Practice&gt; let totals = (sum[ 1 | index &lt;- [1..(max (length str1) (length str2))], (str1!!index == str2!!index || str1!!index == '$')] == (max (length str1) (length str2)))
  *Practice&gt; totals
  *** Exception: Prelude.(!!): index too large
  </code></pre>
  
  <p>I've been doing research but haven't found any solution to this particular error. If anyone knows anything about it, I would most appreciate it.</p>
  
  <p>(Btw, the "index" in the error is not the same as the index I use in the function)</p>