Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rotN :: forall a. (Bounded a, Enum a) => a -> a
  2. rotN c = toEnum rotation
  3.  where alphabetSize = 1 + fromEnum (maxBound :: a)
  4.        halfAlphabet = alphabetSize `div` 2
  5.        offset = fromEnum c + halfAlphabet
  6.        rotation =  offset `mod` alphabetSize
  7.  
  8. rotNdecoder :: forall a. (Bounded a, Enum a) => a -> a
  9. rotNdecoder c = toEnum rotation
  10.  where n = 1 + fromEnum (maxBound :: a)
  11.        halfN = n `div` 2
  12.        offset = if even n
  13.                 then fromEnum c + halfN
  14.                 else 1 + fromEnum c + halfN
  15.        rotation =  offset `mod` n
  16.  
  17. rotEncoder :: (Bounded a, Enum a) => [a] -> [a]
  18. rotEncoder text = map rotN text
  19.  
  20. rotDecoder :: (Bounded a, Enum a) => [a] -> [a]
  21. rotDecoder text = map rotNdecoder text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement