Vigenere cipher in Haskell I implemented the Vigenere cipher in haskell as a part of exercises for  Haskell programming from first principles.

Though the code isn't all that long, I feel it could use some refactoring to simplify things (I feel the code I wrote is ugly). One caveat: string inputs should all be in uppercase.

The vigenere function should be applied like so:

vignere "ALLY" "MEET AT DAWN"


which will create the output:

"MPPRAEOYWY"


As it stands, here's the code I implemented:

import Data.Char

-- exercise chapter 11
encode :: Char -&gt; Int
encode x = ord x - ord 'A'

decode :: Int -&gt; Char
decode x = chr (x + ord 'A')

shift :: (Int -&gt; Int -&gt; Int) -&gt; Int -&gt; Char -&gt; Char
shift f x ch =  decode $ f (encode ch) x `mod` 26

rightShift :: Int -&gt; Char -&gt; Char
rightShift = shift (+)

leftShift :: Int -&gt; Char -&gt; Char
leftShift = shift (-)

encodeString :: String -&gt; [Int]
encodeString str = map encode str

type Secret = String
type PlainText = String
type CipherText = String

vignereString :: Secret -&gt; PlainText -&gt; String
vignereString secret plain = take len $ cycle secret
                          where len = length $ concat $ words plain

vignereCode :: Secret -&gt; PlainText -&gt; [Int]
vignereCode secret plain = encodeString $ vignereString secret plain


vignere :: Secret -&gt; PlainText -&gt; CipherText
vignere secret plain =
  zipWith rightShift code plainNoSpace
  where code = vignereCode secret plain
        plainNoSpace = concat $ words plain

unvignere :: Secret -&gt; CipherText -&gt; PlainText
unvignere secret cipher =
  zipWith leftShift code cipherNoSpace
  where code = vignereCode secret cipher
        cipherNoSpace = concat $ words cipher
It's a nice solution, but very long for a relatively simple task.

First, note that the cipher is called "Vigenère", so vignere would be considered a misspelling.

The type definitions…


type Secret = String
type PlainText = String
type CipherText = String



… provide an illusion of type safety, where there actually isn't any.  They are all just Strings.

All of the helper functions (encode, decode, shift, rightShift, leftShift, and encodeString) could just be reduced to a single function:

import Data.Char (chr, ord)

shift :: (Int -&gt; Int -&gt; Int) -&gt; Char -&gt; Char -&gt; Char
shift op offset ch = numToChar $ (charToNum ch) `op` (charToNum offset)
  where
    charToNum ch = ord ch - ord 'A'
    numToChar n = chr $ (n `mod` 26) + ord 'A'


Hiding the charToNum and numToChar helpers makes it easier to see what calls what.

With the generalized shift function defined, the enciphering and deciphering routines could each be one-liners:

vigenere :: String -&gt; String -&gt; String
vigenere secret = zipWith (shift (+)) (cycle secret) . concat . words

unvigenere :: String -&gt; String -&gt; String
unvigenere secret = zipWith (shift (-)) (cycle secret) . concat . words


Here, I've written them using point-free style to emphasize the fact that vigenere secret acts as an enciphering filter on the text.