初识Haskell 六:IO

对Haskell中涉及IO处理的部分进行总结


IO在Haskell中有自己的类型,即IO(),输入和输出都是IO()

 

IO函数

putChar :: Char -> IO()

putStr :: String -> IO()

print :: Show a => a -> IO()

getChar :: IO Char

getLine :: IO String

interact :: (String -> String) -> IO()

(>>) :: IO a -> IO b ->  IO b --将2个IO操作串联,抛弃第1个IO操作的结果

(>>=) :: IO a -> (a -> IO b) -> IO b --将2个IO操作串联,第1个IO操作的结果传到第2个

File IO

其中type FilePath = String

readFile :: FilePath -> IO String

writeFile :: FilePath -> String -> IO ()

appendFile :: FilePath -> String -> IO () --如:

  appendFile "squares.txt" (show [(x, x*x) | x <- [1..9]])

 

do notation

对于一串IO操作,用(>>)或(>>=)过于繁琐,可写于do中,注意Haskell是layout-sensitive的。

 

posted @ 2020-06-13 17:18  落星无尘_Will  阅读(840)  评论(0编辑  收藏  举报