so far what we have dealt with are all functions and streams. now we might get our hand wet dealing with Files and Streams.
what we will discusses in this post includes:
getContent and getChar ....
getContents is lazy which means it won't read content until we actually use the content read from the call. (how is the laziness implemented)
let's see first one example.
import Data.Char main = do main2 -- contents <- getContents -- putStr (map toUpper contents) main2 = do contents <- getContents putStr (shortLinesOnly contents) shortLinesOnly :: String -> String shortLinesOnly input = let allLines = lines input shortLines = filter (\line -> length line < 10) allLines result = unlines shortLines in result
and again yet another example on how to use getContent to transform lines to upper case.
-- file -- files_streams.hs -- descrpition: -- Files and Streams import Control.Monad import Data.Char main = forever $ do putStr "Give me some input: " l <- getLine putStrLn $ map toUpper l -- with getContents main2 = do contents <- getContents putStr (map toUpper contents)
interact
This pattern of getting some string from the input, transforming it with a function and then outputting that is so common that there exists a function which makes that even easier, called interact.
below are some example with interact.
case 1)
main = interact shortLinesOnly shortLinesOnly :: String -> String shortLinesOnly input = let allLines = lines input shortLines = filter (\line -> length line < 10) allLines result = unlines shortLines in result
and
case 2)
main2 = interact $ unlines . filter ((<10) . length) . lines
and case 3)
respondPalindromes = unlines . map (\xs -> if isPalindrome xs then "palindrome" else "not a palindrome") . lines where isPalindrome xs = xs == reverse xs main = interact respondPalindromes
Now, let's deal with files.
so before start, suppose the runtime has define the following types and type synonyms.
type FilePath = String data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode
hGetContents, hClose
we get contents from files handles with hGetContents call. hClose close a file handle.
withFile
it is so common that you will first open a file, get the contents (read) or write the contents, and close the file , so there is a withFile call.
-- file -- hGetContents_io.hs -- descrpition: -- unlike the getContents_io.hs , which automatically read from the stdin, gGetContents will read from aHandle import Data.Char import System.IO main = do withFile "girlfriend.txt" ReadMode (\handle -> do contents <- hGetContents handle putStr contents) -- haskell's way of withFile pattern, which is more like the Dispose pattern used quit common in C# language -- and if you will implements the code of hGetHandle it is like this: withFile' :: FilePath -> IOMode -> (Handle -> IO a) -> IO a withFile' path mode f = do handle <- openFile path mode result <- f handle hClose handle return result
hGetLine, hPutStr, hPutStrLn, hGetChar
they are like the counterPart without the h... we will ignore this part.
readFile and writeFile, appendFile
readFile, writeFile and appendFile which takes a string to write to that file and returns an I/O action that will do the writing.
readFile
main = do contents <- readFile "girlfriend.txt" putStr contents
writeFile
import System.IO import Data.Char main = do contents <- readFile "girlfriend.txt" writeFile "girlfriendcaps.txt" (map toUpper contents)
appendFile
import System.IO main = do todoItem <- getLine appendFile "todo.txt" (todoItem ++ "\n")
hSetBuffering
you can control the lower-level behavior. here is the code shows that you can control the behavior with the hSetBuffering
-- file -- hSetBuffering_io.hs -- descrpition: -- It takes a handle and a BufferMode and returns an I/O action that sets the buffering. It takes a handle and a BufferMode and returns an I/O action that sets the buffering. -- an example of the value to the hSetBuffering is like this: -- NoBuffering, LineBuffering or BlockBuffering (Maybe Int). -- import System.IO main = do withFile "something.txt" ReadMode (\handle -> do hSetBuffering handle $ BlockBuffering (Just 2048) contents <- hGetContents handle putStr contents) -- you may as well try the hFush function which will flush the buffers that is in the end writeBuffer
openTempFile, hFlush
an example use the following...
-- file -- remove_todo.hs -- description: -- remove some items from the todo List import System.IO import System.Directory import Data.List main = do handle <- openFile "todo.txt" ReadMode (tempName, tempHandle) <- openTempFile "." "temp" contents <- hGetContents handle let todoTasks = lines contents numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks putStrLn "These are your TO-DO items:" putStr $ unlines numberedTasks putStrLn "Which one do you want to delete?" numberString <- getLine let number = read numberString newTodoItems = delete (todoTasks !! number) todoTasks hPutStr tempHandle $ unlines newTodoItems hClose handle hClose tempHandle removeFile "todo.txt" -- remove a file from the FS renameFile tempName "todo.txt" -- rename A file to B file
相关推荐
Atom-haskell-ghc-mod.zip,haskell-ghc-mod atom packagehaskell ghc mod atom包,atom是一个用web技术构建的开源文本编辑器。
haskell-mode emacs haskell-mode emacs
Atom-ide-haskell-hoogle.zip,在光标下显示符号的滚动信息艾德·哈斯克尔·胡格尔,atom是一个用web技术构建的开源文本编辑器。
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
Programming-in-Haskell-2nd-Edition.pdf
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...
Get Programming with HASKELL-2018-英文版
用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...
演示医疗用例的参考DAML应用程序 -Haskell-TypeScript-下载
haskell-chart, haskell的2D 图表库 图 haskell的2D 图表库进一步的信息可以在关联的 wiki中找到。
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
A History of Haskell - Being Lazy With Class
haskell-formatter --force --input a.hs --output a.hs hindent a.hs 就地格式化多个文件 hindent a.hs b.hs 将stdin格式化为stdout haskell-formatter hindent 订单进口 完成 hindent --sort-imports … ...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...
haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...
Atom-atom-haskell-scry.zip,De-emphasize qualified Haskell identifiers.SCRY,atom是一个用web技术构建的开源文本编辑器。
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid 已加书签