浏览代码

Merge branch 'io'

DricomDragon 5 年之前
父节点
当前提交
334301d06d
共有 4 个文件被更改,包括 46 次插入0 次删除
  1. 24 0
      io_cesar.hs
  2. 8 0
      io_length.hs
  3. 7 0
      io_rev.hs
  4. 7 0
      io_shuffle.hs

+ 24 - 0
io_cesar.hs

@@ -0,0 +1,24 @@
+-- Slide 58
+
+import Data.Char
+
+cechar n c = chr ((mod ((ord c) + n - ord 'a') ((ord 'z') - ord 'a'  + 1)) + ord 'a')
+
+cesar n = map (cechar n)
+
+inlinecesar n s = map (chr.(+ ord 'a').(flip mod 26).(+ n).(subtract (ord 'a')).ord) s
+
+getCorrectValue::IO Int
+getCorrectValue = do
+	putStrLn "Please give a number in [1, 26["
+	x <- getLine
+	let i = (read x)::Int
+	if i > 0 && i < 26 then return i else getCorrectValue
+
+main::IO ()
+main = do
+	n <- getCorrectValue
+	putStrLn "Please give a word"
+	s <- getLine
+	putStrLn (cesar n s)
+	putStrLn (inlinecesar n s)

+ 8 - 0
io_length.hs

@@ -0,0 +1,8 @@
+-- Slide 57
+
+ioLength::IO Int
+ioLength = do
+	x <- getLine
+	return (length x)
+
+main = ioLength >>= print

+ 7 - 0
io_rev.hs

@@ -0,0 +1,7 @@
+-- Slide 55
+-- It makes more sense to compile this file
+
+import Data.List
+
+main::IO()
+main = getLine >>= putStrLn.(\x -> concat [x, " ", reverse x])

+ 7 - 0
io_shuffle.hs

@@ -0,0 +1,7 @@
+-- Slide 55
+-- It makes more sense to compile this file
+
+import Data.List
+
+main::IO()
+main = getLine >>= (\x -> getLine >>= (\y -> putStrLn (y ++ x)))