logo资料库

Haskell趣学指南.pdf

第1页 / 共426页
第2页 / 共426页
第3页 / 共426页
第4页 / 共426页
第5页 / 共426页
第6页 / 共426页
第7页 / 共426页
第8页 / 共426页
资料共426页,剩余部分请下载后查看
第一章 简介
§1.1关于这份教学
§1.2什么是 Haskell?
§1.3你需要些什么来使用 Haskell 呢?
第二章 从零开始
§2.1准备好了吗?
§2.2初学者的第一个函数
§2.3List 入门
§2.4使用 Range
§2.5List Comprehension
§2.6Tuple
第三章 Types and Typeclasses
§3.1Type
§3.2Type variables
§3.3Typeclasses入门
第四章 函数的语法
§4.1模式匹配 (Pattern matching)
§4.2什么是 Guards
§4.3关键字 Where
§4.4关键字 Let
§4.5Case expressions
第五章 递回
§5.1你好,递回!
§5.2实现 Maximum
§5.3来看几个递回函数
§5.4``快速''排序
§5.5用递回来思考
第六章 高阶函数
§6.1Curried functions
§6.2是时候了,来点高阶函数!
§6.3map 与 filter
§6.4lambda
§6.5关键字 fold
§6.6有$的函数调用
§6.7Function composition
第七章 模组 (Modules)
§7.1装载模组
§7.2Data.List
§7.3Data.Char
§7.4Data.Map
§7.5Data.Set
§7.6建立自己的模组
第八章 构造我们自己的 Types 和 Typeclasses
§8.1Algebraic Data Types 入门
§8.2Record Syntax
§8.3Type parameters
§8.4Derived instances
§8.5Type synonyms
§8.6Recursive data structures (递回地定义数据结构)
§8.7Typeclasses 的第二堂课
§8.8yes-no typeclass
§8.9Functor typeclass
§8.10Kind
第九章 输入与输出
§9.1Hello, world!
§9.2文件与字符流
§9.3命令行引数
§9.4乱数
§9.5Bytestrings
§9.6Exceptions (异常)
第十章 函数式地思考来解决问题
§10.1运算逆波兰表示法(Reverse Polish notation form)
§10.2路径规划
第十一章 Functors, Applicative Functors 与 Monoids
§11.1温习 Functors
§11.2Applicative functors
§11.3关键字``newtype''
11.3.1 Using newtype to make type class instances
11.3.2 On newtype laziness
11.3.3 type vs newtype vs data
§11.4Monoids
11.4.1 Lists are monoids
11.4.2 Product and Sum
11.4.3 Any and ALL
11.4.4 The Ordering monoid
11.4.5 Maybe the monoid
11.4.6 Using monoids to fold data structures
第十二章 来看看几种 Monad
§12.1动手做做看: Maybe Monad
§12.2Monad type class
§12.3走钢索
§12.4do 表示法
§12.5List Monad
12.5.1 A knight's quest
§12.6Monad laws (单子律)
12.6.1 Left identity
12.6.2 Right identity
12.6.3 Associativity
第十三章 再来看看更多 Monad
§13.1你所不知道的 Writer Monad
13.1.1 Monoids 的好处
13.1.2 The Writer type
13.1.3 Using do notation with Writer
13.1.4 Adding logging to programs
13.1.5 Inefficient list construction
13.1.6 Difference lists
13.1.7 Comparing Performance
§13.2Reader Monad
§13.3State Monad
13.3.1 Stack and Stones
13.3.2 The State Monad
13.3.3 随机性与 state monad
§13.4Error Monad
§13.5一些实用的 Moandic functions
13.5.1 liftM
13.5.2 The join function
13.5.3 filterM
13.5.4 foldM
13.5.5 Making a safe RPN calculator
13.5.6 Composing monadic functions
§13.6定义自己的 Monad
第十四章 Zippers 数据结构
§14.1来走二元树吧!
§14.2凡走过必留下痕迹
14.2.1 Going back up
14.2.2 Manipulating trees under focus
14.2.3 I'm going straight to top, oh yeah, up where the air is fresh and clean!
§14.3来看串行
§14.4阳春的文件系统
14.4.1 A zipper for our file system
14.4.2 Manipulating our file system
§14.5小心每一步
附录 A FAQ
§A.1我能把这份教学放在我的网站吗?我可以更改里面的内容吗?
§A.2推荐其它几个 Haskell 读物?
§A.3我怎么联络到你?
§A.4我想要一些习题!
§A.5关于作者
§A.6关于简体译者
§A.7关于繁体译者
附录 B Resource
§B.1Specification
§B.2Tools
§B.3Lectures & Articles
§B.4Forum
§B.5Online Judge
§B.6Books
§B.7PL Researchers
§B.8Interesting Projects
§B.9Taiwan Functional Programming User Group
Haskell 趣学指南 M. Lipovaca (英) MnO2 (繁) 开源书屋 (简) 2013 年 10 月 3 日
目录 第一章 简介 §1.1 关于这份教学 . . . . . . . . . . . . . . . . . . . . . . . . . . . §1.2 什么是 Haskell? . . . . . . . . . . . . . . . . . . . . . . . . . §1.3 你需要些什么来使用 Haskell 呢? . . . . . . . . . . . . . . . . 第二章 从零开始 §2.1 准备好了吗? . . . . . . . . . . . . . . . . . . . . . . . . . . . §2.2 初学者的第一个函数 . . . . . . . . . . . . . . . . . . . . . . . §2.3 List 入门 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . §2.4 使用 Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . §2.5 List Comprehension . . . . . . . . . . . . . . . . . . . . . . . §2.6 Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 第三章 Types and Typeclasses §3.1 Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . §3.2 Type variables . . . . . . . . . . . . . . . . . . . . . . . . . . §3.3 Typeclasses 入门 . . . . . . . . . . . . . . . . . . . . . . . . . 第四章 函数的语法 §4.1 模式匹配 (Pattern matching) . . . . . . . . . . . . . . . . . . §4.2 什么是 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . §4.3 关键字 Where . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . §4.4 关键字 Let §4.5 Case expressions . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 2 4 6 6 10 12 17 20 23 27 27 30 31 37 37 42 45 47 49
第 ii 页 第五章 递回 目录 Haskell 趣学指南 §5.1 你好,递回! . . . . . . . . . . . . . . . . . . . . . . . . . . . §5.2 实现 Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . §5.3 来看几个递回函数 . . . . . . . . . . . . . . . . . . . . . . . . §5.4 “快速” 排序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . §5.5 用递回来思考 . . . . . . . . . . . . . . . . . . . . . . . . . . . 第六章 高阶函数 §6.1 Curried functions . . . . . . . . . . . . . . . . . . . . . . . . . §6.2 是时候了,来点高阶函数! . . . . . . . . . . . . . . . . . . . §6.3 map 与 filter . . . . . . . . . . . . . . . . . . . . . . . . . . . §6.4 lambda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . §6.5 关键字 fold . . . . . . . . . . . . . . . . . . . . . . . . . . . . §6.6 有 $ 的函数调用 . . . . . . . . . . . . . . . . . . . . . . . . . §6.7 Function composition . . . . . . . . . . . . . . . . . . . . . . 第七章 模组 (Modules) §7.1 装载模组 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . §7.2 Data.List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . §7.3 Data.Char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 52 53 54 57 59 61 61 64 67 72 74 80 81 85 85 87 99 §7.4 Data.Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 §7.5 Data.Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 §7.6 建立自己的模组 . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Haskell 趣学指南 目录 第八章 构造我们自己的 Types 和 Typeclasses 第 iii 页 117 §8.1 Algebraic Data Types 入门 . . . . . . . . . . . . . . . . . . . 117 §8.2 Record Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 121 §8.3 Type parameters . . . . . . . . . . . . . . . . . . . . . . . . . 124 §8.4 Derived instances . . . . . . . . . . . . . . . . . . . . . . . . . 130 §8.5 Type synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . 135 §8.6 Recursive data structures (递回地定义数据结构) . . . . . . . 140 §8.7 Typeclasses 的第二堂课 . . . . . . . . . . . . . . . . . . . . . 145 §8.8 yes-no typeclass . . . . . . . . . . . . . . . . . . . . . . . . . . 150 §8.9 Functor typeclass . . . . . . . . . . . . . . . . . . . . . . . . . 153 §8.10 Kind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 第九章 输入与输出 163 §9.1 Hello, world! . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 §9.2 文件与字符流 . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 §9.3 命令行引数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 §9.4 乱数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 §9.5 Bytestrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 §9.6 Exceptions (异常) . . . . . . . . . . . . . . . . . . . . . . . . 218 第十章 函数式地思考来解决问题 227 §10.1 运算逆波兰表示法 (Reverse Polish notation form) . . . . . . 227 §10.2 路径规划 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
第 iv 页 目录 Haskell 趣学指南 第十一章 Functors, Applicative Functors 与 Monoids 243 §11.1 温习 Functors . . . . . . . . . . . . . . . . . . . . . . . . . . 243 §11.2 Applicative functors . . . . . . . . . . . . . . . . . . . . . . . 257 §11.3 关键字 “newtype” . . . . . . . . . . . . . . . . . . . . . . . . 277 11.3.1 Using newtype to make type class instances . . . . . . 280 11.3.2 On newtype laziness . . . . . . . . . . . . . . . . . . . 282 11.3.3 type vs newtype vs data . . . . . . . . . . . . . . . . . 284 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 11.4.1 Lists are monoids . . . . . . . . . . . . . . . . . . . . . 289 11.4.2 Product and Sum . . . . . . . . . . . . . . . . . . . . . 290 11.4.3 Any and ALL . . . . . . . . . . . . . . . . . . . . . . . 292 11.4.4 The Ordering monoid . . . . . . . . . . . . . . . . . . 293 11.4.5 Maybe the monoid . . . . . . . . . . . . . . . . . . . . 297 11.4.6 Using monoids to fold data structures . . . . . . . . . 299 §11.4 Monoids 第十二章 来看看几种 Monad 304 §12.1 动手做做看: Maybe Monad . . . . . . . . . . . . . . . . . . . 306 §12.2 Monad type class . . . . . . . . . . . . . . . . . . . . . . . . 309 §12.3 走钢索 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 §12.4 do 表示法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 §12.5 List Monad . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 . . . . . . . . . . . . . . . . . . . . . 333 §12.6 Monad laws (单子律) . . . . . . . . . . . . . . . . . . . . . . 336 12.6.1 Left identity . . . . . . . . . . . . . . . . . . . . . . . 336 12.6.2 Right identity . . . . . . . . . . . . . . . . . . . . . . . 337 12.6.3 Associativity . . . . . . . . . . . . . . . . . . . . . . . 338 12.5.1 A knight’s quest
Haskell 趣学指南 目录 第十三章 再来看看更多 Monad 第 v 页 341 §13.1 你所不知道的 Writer Monad . . . . . . . . . . . . . . . . . . 342 13.1.1 Monoids 的好处 . . . . . . . . . . . . . . . . . . . . . 344 13.1.2 The Writer type . . . . . . . . . . . . . . . . . . . . . 346 13.1.3 Using do notation with Writer . . . . . . . . . . . . . 348 13.1.4 Adding logging to programs . . . . . . . . . . . . . . . 350 13.1.5 Inefficient list construction . . . . . . . . . . . . . . . 352 13.1.6 Difference lists . . . . . . . . . . . . . . . . . . . . . . 353 13.1.7 Comparing Performance . . . . . . . . . . . . . . . . . 355 §13.2 Reader Monad . . . . . . . . . . . . . . . . . . . . . . . . . . 356 §13.3 State Monad . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 13.3.1 Stack and Stones . . . . . . . . . . . . . . . . . . . . . 361 13.3.2 The State Monad . . . . . . . . . . . . . . . . . . . . . 362 13.3.3 随机性与 state monad . . . . . . . . . . . . . . . . . . 366 §13.4 Error Monad . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 §13.5 一些实用的 Moandic functions . . . . . . . . . . . . . . . . . 369 13.5.1 liftM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 13.5.2 The join function . . . . . . . . . . . . . . . . . . . . . 373 13.5.3 filterM . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 13.5.4 foldM . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 13.5.5 Making a safe RPN calculator . . . . . . . . . . . . . . 380 13.5.6 Composing monadic functions . . . . . . . . . . . . . . 384 §13.6 定义自己的 Monad . . . . . . . . . . . . . . . . . . . . . . . 386
第 vi 页 目录 Haskell 趣学指南 第十四章 Zippers 数据结构 393 . . . . . . . . . . . . . . . . . . . . . . . . . . 394 §14.1 来走二元树吧! §14.2 凡走过必留下痕迹 . . . . . . . . . . . . . . . . . . . . . . . . 397 14.2.1 Going back up . . . . . . . . . . . . . . . . . . . . . . 398 14.2.2 Manipulating trees under focus . . . . . . . . . . . . . 401 14.2.3 I’m going straight to top, oh yeah, up where the air is fresh and clean! . . . . . . . . . . . . . . . . . . . . . . 402 §14.3 来看串行 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 §14.4 阳春的文件系统 . . . . . . . . . . . . . . . . . . . . . . . . . 405 14.4.1 A zipper for our file system . . . . . . . . . . . . . . . 406 14.4.2 Manipulating our file system . . . . . . . . . . . . . . 409 §14.5 小心每一步 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 附录 A FAQ 414 §A.1 我能把这份教学放在我的网站吗?我可以更改里面的内容吗? 414 §A.2 推荐其它几个 Haskell 读物? . . . . . . . . . . . . . . . . . . 414 §A.3 我怎么联络到你? . . . . . . . . . . . . . . . . . . . . . . . . 414 §A.4 我想要一些习题! . . . . . . . . . . . . . . . . . . . . . . . . 414 §A.5 关于作者 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 §A.6 关于简体译者 . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 §A.7 关于繁体译者 . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 附录 B Resource 416 §B.1 Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 §B.2 Tools §B.3 Lectures & Articles . . . . . . . . . . . . . . . . . . . . . . . 416
Haskell 趣学指南 目录 第 vii 页 §B.4 Forum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 §B.5 Online Judge . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 §B.6 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 §B.7 PL Researchers . . . . . . . . . . . . . . . . . . . . . . . . . . 418 §B.8 Interesting Projects . . . . . . . . . . . . . . . . . . . . . . . 418 §B.9 Taiwan Functional Programming User Group . . . . . . . . . 418
分享到:
收藏