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 ...
随机推荐
- SCCM2012 R2实战系列之六:安装客户端代理软件
在安装客户端代理软件之前,请大家确保已经对本系列的第四和第五部分有了基本了解,而且对SCCM环境做了初始化配置和发现方法.我们目前讨论的是加域的计算机,对于工作组的计算机还需要进行额外的配置.在上篇文 ...
- Glow 效果材质
转自:http://blog.csdn.net/panda1234lee/article/details/60960846 算法较简单,首先来看 Base color 部分: 就是将对事先准备好的三张 ...
- XrmToolBox 连接
- CRM stringmap
CREATE view [dbo].[V_stringmap] as SELECT DISTINCT Entity.Name as tablename,StringMap.AttributeName ...
- springboot获取application.yml中的配置信息
HelloController.java package com.springbootweb.demo.controller; import com.springbootweb.demo.entity ...
- 1-Lombok工具使用
1.先给Eclispe安装Lombok插件 首先下载 lombok-1.16.6.jar 这个jar包 存放在系统任意盘里面 我这里是放在 c:\lombok-1.16.6.jar 运行命令 点 ...
- 第11章 拾遗5:IPv6和IPv4共存技术(1)_双栈技术和6to4隧道技术
6. IPv6和IPv4共存技术 6.1 双栈技术 (1)双协议主机的协议结构 (2)双协议栈示意图 ①双协议主机在通信时首先通过支持双协议的DNS服务器查询与目的主机名对应的IP地址. ②再根据指定 ...
- Linux后台有个systemd-r进程,占用5355等端口
编辑配置文件 vim /etc/systemd/resolved.conf 设置LLMNR=0 重启服务: systemctl restart systemd-resolved.service
- Markdown画各种图表
并不是所有编辑器都支持,比如博客园这个就不支持... 流程图 st=>start: 开始 op=>operation: 首先按个按钮 op2=>operation: 那你从头开始吧 ...
- JS面试Q&A(续):Javascript数组排序, 默认是字符串Unicode排序, 不适合数字
Q:下面代码段的执行后data里面的数据是什么?为什么? var data= [40,1,5,200] data.sort(); A: data的内容是[1, 200, 40, 5] 因为,Javas ...