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.0.5 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.0.4 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.0.3 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.0.0.2 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-2.0.0.1 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-2.0.0.0 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.4.1 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.4.0 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.3.0 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.2.1 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.2.0 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.1.2 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.1.1 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.1.0 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.0.1 tracks/haskell/exercises/simple-cipher/src/Example.hs
trackler-1.0.0 tracks/haskell/exercises/simple-cipher/src/Example.hs