Sha256: e28b0c0c433bdfcdcb57b09a231e69b3789cb41d4beab9af8dad3c92f304f23b

Contents?: true

Size: 784 Bytes

Versions: 396

Compression:

Stored size: 784 Bytes

Contents

module Cipher (caesarEncode, caesarDecode, caesarEncodeRandom) where

import Data.Char     (isAsciiLower)
import System.Random (newStdGen, randomRs)

caesarEncode, caesarDecode :: String -> String -> String
caesarEncode = caesar (+)
caesarDecode = caesar (-)

caesarEncodeRandom :: String -> IO (String, String)
caesarEncodeRandom xs = do
  g <- newStdGen
  let ks = take (length xs) (randomRs ('a', 'z') g)
  return (ks, caesarEncode ks xs)

caesar :: (Int -> Int -> Int) -> String -> String -> String
caesar op = zipWith rotate . cycle
  where
    rotate k x = if isAsciiLower k && isAsciiLower x
                then toAlpha (op (fromAlpha x) (fromAlpha k) `mod` 26)
                else x
    fromAlpha = subtract (fromEnum 'a') . fromEnum
    toAlpha = toEnum . (fromEnum 'a' +)

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.97 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.96 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.95 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.94 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.93 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.92 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.91 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.90 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.89 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.88 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.87 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.86 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.85 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.84 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.83 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.82 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.81 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.80 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.79 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs