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.0.6.0 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.18 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.17 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.16 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.15 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.14 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.13 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.12 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.11 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.10 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.9 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.8 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.7 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.6 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.5 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.4 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.3 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.2 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.1 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.5.0 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs