没有合适的资源?快使用搜索试试~ 我知道了~
Haskell: Functional Programming with Types
4星 · 超过85%的资源 需积分: 10 26 下载量 8 浏览量
2016-11-22
22:14:42
上传
评论
收藏 5.13MB PDF 举报
温馨提示
试读
607页
Haskell: Functional Programming with Types
资源推荐
资源详情
资源评论
Haskell
en.wikibooks.org
April 20, 2014
On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia
projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A
URI to this license is given in the list of figures on page 589. If this document is a derived work
from the contents of one of these projects and the content was still licensed by the project under
this license at the time of derivation this document has to be licensed under the same, a similar or a
compatible license, as stated in section 4b of the license. The list of contributors is included in chapter
Contributors on page 585. The licenses GPL, LGPL and GFDL are included in chapter Licenses on
page 593, since this book and/or parts of it may or may not be licensed under one or more of these
licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of
figures on page 589. This PDF was generated by the L
A
T
E
X typesetting software. The L
A
T
E
X source
code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from
the PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www.
pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save
the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z.
To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The L
A
T
E
X
source itself was generated by a program written by Dirk Hünniger, which is freely available under
an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf.
Contents
1 Haskell Basics 3
2 Getting set up 5
2.1 Installing Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Very first steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Variables and functions 9
3.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Haskell source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.4 Variables in imperative languages . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.6 Local definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 Truth values 19
4.1 Equality and other comparisons . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2 Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.3 Infix operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.4 Boolean operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.5 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5 Type basics 27
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.2 Using the interactive :type command . . . . . . . . . . . . . . . . . . . . 28
5.3 Functional types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.4 Type signatures in code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6 Lists and tuples 39
6.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.3 Retrieving values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.4 Polymorphic types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7 Type basics II 49
7.1 The Num class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.2 Numeric types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7.3 Classes beyond numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
III
Contents
8 Building vocabulary 55
8.1 Function composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.2 The need for a vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.3 Prelude and the hierarchical libraries . . . . . . . . . . . . . . . . . . . . . 57
8.4 One exhibit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.5 Acquiring vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
9 Next steps 61
9.1 if / then / else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.2 Introducing pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.3 Tuple and list patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
9.4 let bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
10 Simple input and output 69
10.1 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10.2 Actions under the microscope . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.3 Learn more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
11 Elementary Haskell 79
12 Recursion 81
12.1 Numeric recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
12.2 List-based recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.3 Don't get TOO excited about recursion... . . . . . . . . . . . . . . . . . . 88
12.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
13 More about lists 89
13.1 Rebuilding lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
13.2 Generalizing even further . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.3 The map function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
13.4 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
14 List processing 97
14.1 Folds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.2 Scans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
14.3 filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
14.4 List comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
15 Type declarations 107
15.1 data and constructor functions . . . . . . . . . . . . . . . . . . . . . . . . 107
15.2 Deconstructing types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
15.3 type for making type synonyms . . . . . . . . . . . . . . . . . . . . . . . . 110
16 Pattern matching 113
16.1 Analysing pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . . 113
16.2 The connection with constructors . . . . . . . . . . . . . . . . . . . . . . . 114
16.3 Matching literal values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
16.4 Syntax tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
16.5 Where we can use pattern matching . . . . . . . . . . . . . . . . . . . . . 118
IV
Contents
17 Control structures 121
17.1 if and guards revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
17.2 case expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
17.3 Controlling actions, revisited . . . . . . . . . . . . . . . . . . . . . . . . . . 124
18 More on functions 127
18.1 let and where revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
18.2 Anonymous Functions - lambdas . . . . . . . . . . . . . . . . . . . . . . . 128
18.3 Operators and sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
19 Higher order functions and Currying 131
19.1 The Quickest Sorting Algorithm In Town . . . . . . . . . . . . . . . . . . . 131
19.2 Now, How Do We Use It? . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
19.3 Tweaking What We Already Have . . . . . . . . . . . . . . . . . . . . . . . 133
19.4 quickSort, Take Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
19.5 But What Did We Gain? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
19.6 Higher-Order Functions and Types . . . . . . . . . . . . . . . . . . . . . . 134
19.7 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
19.8 Function manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
20 Using GHCi effectively 139
20.1 User interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
21 Intermediate Haskell 141
22 Modules 143
22.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
22.2 Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
22.3 Exporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
23 Indentation 149
23.1 The golden rule of indentation . . . . . . . . . . . . . . . . . . . . . . . . . 149
23.2 A mechanical translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
23.3 Layout in action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
24 More on datatypes 155
24.1 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
24.2 Named Fields (Record Syntax) . . . . . . . . . . . . . . . . . . . . . . . . 155
24.3 Parameterized Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
25 Other data structures 161
25.1 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
25.2 Other datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
26 Classes and types 169
26.1 Classes and instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
26.2 Deriving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
26.3 Class inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
26.4 Standard classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
V
剩余606页未读,继续阅读
资源评论
- 千与2017-09-22不错的分享,内容很清晰,谢谢分享
IMT_23
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功