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.139 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.138 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.137 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.136 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.135 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.134 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.133 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.132 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.131 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.130 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.129 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.128 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.127 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.126 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.125 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.124 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.123 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.122 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.121 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs
trackler-2.2.1.120 tracks/haskell/exercises/simple-cipher/examples/success-standard/src/Cipher.hs