Haskell语言学习笔记(19)File IO
关于IO Action
- 类型为IO t。
- 运算时不执行,因而没有任何效果,只有执行时才会有效果,产生副作用。
- 一个IO Action只有在其他IO Action中才能被执行。
- 类型为IO t的IO Action被执行后的结果类型为t。
File IO 函数
- openFile hClose 打开和关闭文件
readMode, writeMode, readwriteMode, appendMode - hTell hSeek 文件当前读取位置
AbsoluteSeek, RelativeSeek, SeekFromEnd - withFile 用回调函数读取文件
- readFile writeFile 直接读取写入文件
import System.IO inputPath = "input.txt"
openFile1 = do
handle <- openFile inputPath ReadMode
contents <- hGetContents handle
putStr contents
hClose handle
withFile1 = do
withFile inputPath ReadMode (\handle -> do
contents <- hGetContents handle
putStr contents)
readFile1 = do
contents <- readFile inputPath
putStr contents
以上代码使用三种不同的方法从文件中读取内容并打印到屏幕上。
UTF-8文件的读写
readWriteUTF8File = do
inputHandle <- openFile inputPath ReadMode
hSetEncoding inputHandle utf8
theInput <- hGetContents inputHandle
outputHandle <- openFile outputPath WriteMode
hSetEncoding outputHandle utf8
hPutStr outputHandle $ map toUpper theInput
hClose inputHandle
hClose outputHandle
UTF-8文件的读写(2)
安装 extra 模块。
$ cabal install extra
Installed extra-1.6
import Extra
readWriteUTF8File' = do
theInput <- readFileEncoding inputPath utf8
writeFileEncoding utf8 outputPath $ upper theInput
Haskell语言学习笔记(19)File IO的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- Haskell语言学习笔记(86)字符串格式化与插值
String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串, ...
- Haskell语言学习笔记(85)Async
安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> ...
- Haskell语言学习笔记(84)Concurrent
Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Conc ...
随机推荐
- 关于promise的几个认知
1. 为什么要有promise ···从代码上来说回避了回调嵌套的问题,其次promise可以保留异步请求的状态(即使得到结果不立刻执行回调,过一阵再执行仍然是可以的.) ···从思想上来说,我们设计 ...
- mono搭建脚本整理
一.介绍 mono项目致力于能够使得开发人员在Linux用C#开发程序. 该项目的目标是创建一系列符合标准ECMA (Ecma-334和Ecma-335)的.Net 工具, 包括C #编译器和共同语言 ...
- 基于HAProxy+Keepalived高可用负载均衡web服务的搭建
一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...
- Ren'Py视觉小说安装,玩一下吧,上班很闲的话
---------------------------------------------------------------------------------------------------- ...
- 3-scala高级
1.模式匹配 //①简单表示: sign = ch match { case '+' => 1 case '-' => -1 case '_' => 0 } //②守卫:(case中 ...
- VS2015 无法启动 IIS服务器
打开VS2012解决方案资源管理器 -> 点选 Web 项目选择 -> 属性 -> Web ->创建虚拟目录. 再次运行Web项目,成功.
- ubantu 与Windows 资源共享
Linux与Windows共享文件夹之samba的安装与使用(Ubuntu为例) 作者:@gzdaijie本文为作者原创,转载请注明出处:http://www.cnblogs.com/gzdaij ...
- ERROR: iterator not incrementable || iterator not decrementable
这个错误提示:迭代器不可以增加 exmaple: vector<int> tVecInt; vector<int>::reverse_iterator iterInt = tV ...
- 微信小程序奇奇怪怪的语法
这... <view class="body"> <view class="nav bc_white"> <view class= ...
- sublime text3 汉化
1.sublime text3 下载地址:http://sublimetextcn.com/ 2.安装完毕 进入sublime主页面,点击菜单栏中“preferences”,弹出选项中找到“packa ...