io_cesar.hs 479 B

12345678910111213141516171819202122
  1. -- Slide 58
  2. import Data.Char
  3. cechar n c = chr ((mod ((ord c) + n - ord 'a') ((ord 'z') - ord 'a' + 1)) + ord 'a')
  4. cesar n = map (cechar n)
  5. inlinecesar n s = map (chr.(+ ord 'a').(flip mod 26).(+ n).(subtract (ord 'a')).ord) s
  6. getCorrectValue::IO Int
  7. getCorrectValue = do
  8. x <- getLine
  9. let i = (read x)::Int
  10. if i >= 0 && i < 26 then return i else getCorrectValue
  11. main::IO ()
  12. main = do
  13. n <- getCorrectValue
  14. s <- getLine
  15. putStrLn (cesar n s)
  16. putStrLn (inlinecesar n s)