title: Vigenere Cipher not encoding properly
body: <pre><code>calcAlphaPos :: Char -&gt; Int
  calcAlphaPos a 
      | (a `elem` ['a'..'z']) = ord(a) - ord('a')
      | (a `elem` ['A'..'Z']) = ord(a) - ord('A') 
      | otherwise = 0
  
  calcOffset :: String -&gt; Int -&gt; Int
  calcOffset word pos = calcAlphaPos (word !! (pos `mod` (length word)))
            
  encodex :: Int -&gt; Char -&gt; Char
  encodex offset ch
      | (ch `elem` ['a'..'z']) = chr ((((ord ch - ord ('a')) + offset + 26) `mod` 26) + ord('a'))
      | (ch `elem` ['A'..'Z']) = chr ((((ord ch - ord ('A')) + offset + 26) `mod` 26) + ord('A'))
      | otherwise = ch
  
  encode :: String -&gt; String -&gt; Int -&gt; String
  encode _ [] _ = &quot;...End of message&quot;
  encode [] _ _ = &quot;Invalid decoder word!&quot;
  encode word (x:xs) y
      | (word !! wordpos) `elem` ['a'..'z'] = [encodex (calcOffset word y) x] ++ encode word xs (y+1)
      | (word !! wordpos) `elem` ['A'..'Z'] = [encodex (calcOffset word y) x] ++ encode word xs (y+1)
      | otherwise = [x] ++ encode word xs y
       where wordpos = y `mod` length word
     
  </code></pre>
  <ol>
  <li><p>encode &quot;ALLY&quot; &quot;MEET AT DAWN&quot; 0</p>
  <pre><code>should produce MPPR AE OYWY but produces MPPR LE DLHL
  
  note &quot;ALLY&quot; provides offset of 0, 11, 11, 24
  MEET =                 [12,  4,  4, 19]
  MPPR =                 [12, 15, 15, 17] &lt;-- 43 - 26 = 17
  </code></pre>
  </li>
  </ol>
  <p>I'm learning haskell and have been trying to figure out what I am doing wrong in regards
  to the Vegenere cipher.  When I don't use spaces in the message the cipher works but I can't
  seem to figure out how to get it to work when spaces are included.</p>