io_cesar.hs 553 B

123456789101112131415161718192021222324
  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. putStrLn "Please give a number in [1, 26["
  9. x <- getLine
  10. let i = (read x)::Int
  11. if i > 0 && i < 26 then return i else getCorrectValue
  12. main::IO ()
  13. main = do
  14. n <- getCorrectValue
  15. putStrLn "Please give a word"
  16. s <- getLine
  17. putStrLn (cesar n s)
  18. putStrLn (inlinecesar n s)