Haskell.pdf
(
3831 KB
)
Pobierz
Haskell
Wikibooks.org
May 28, 2012
This PDF 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
.
The list of contributors is included in chapter Contributors on page
569
. The licenses GPL, LGPL
, 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 page579. 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. An URI to this license is given in
the list of figures on page
and GFDL are included in chapter Licenses on page
583
. 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.
579
Contents
1 Haskell Basics
3
2 Getting set up
5
2.1 InstallingHaskell
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
2.2Veryfirststeps
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3 Variables and functions
7
3.1Variables
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.2Haskellsourcefiles
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.3Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
3.4Variablesinimperativelanguages
. . . . . . . . . . . . . . . . . . . 10
3.5Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.6Localdefinitions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
3.7Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
4 Truth values
17
4.1Equalityandothercomparisons. . . . . . . . . . . . . . . . . . . .
17
4.2Booleanvalues
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
4.3 Infixoperators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
4.4Booleanoperations
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
4.5Guards
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5 Type basics
25
5.1 Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
25
5.2Usingtheinteractive
:type
command
. . . . . . . . . . . . . . . . .
26
5.3Functionaltypes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
5.4Typesignaturesincode . . . . . . . . . . . . . . . . . . . . . . . . .
32
6 Lists and tuples
37
6.1Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
6.2Tuples
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
6.3Retrievingvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
6.4Polymorphictypes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
6.5Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
7 Type basics II
47
7.1The
Num
class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
7.2Numerictypes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
48
7.3Classesbeyondnumbers
. . . . . . . . . . . . . . . . . . . . . . . . . . 51
III
Contents
8 Building vocabulary
53
8.1Functioncomposition. . . . . . . . . . . . . . . . . . . . . . . . . . .
53
8.2Theneedforavocabulary
. . . . . . . . . . . . . . . . . . . . . . .
54
8.3Preludeandthehierarchicallibraries . . . . . . . . . . . . . . .
55
8.4Oneexhibit
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
8.5Acquiringvocabulary
. . . . . . . . . . . . . . . . . . . . . . . . . . .
57
9 Next steps
59
9.1 if/then/else
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
59
9.2 Introducingpatternmatching
. . . . . . . . . . . . . . . . . . . . .
60
9.3Tupleandlistpatterns
. . . . . . . . . . . . . . . . . . . . . . . . .
63
9.4
let
bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
10 Simple input and output
67
10.1Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
10.2Actionsunderthemicroscope
. . . . . . . . . . . . . . . . . . . . .
72
10.3Learnmore. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
76
11 Elementary Haskell
77
12 Recursion
79
12.1Numericrecursion
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
12.2List-basedrecursion. . . . . . . . . . . . . . . . . . . . . . . . . . . .
84
12.3Don
'
tgetTOOexcitedaboutrecursion...
. . . . . . . . . . . . .
86
12.4Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
86
13 More about lists
87
13.1Rebuildinglists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
87
13.2Generalizingevenfurther
. . . . . . . . . . . . . . . . . . . . . . . .
89
13.3The
map
function
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.4TipsandTricks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
92
14 List processing
95
14.1Folds
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
95
14.2Scans
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.3filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.4Listcomprehensions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
15 Type declarations
105
15.1
data
andconstructorfunctions
. . . . . . . . . . . . . . . . . . . . 105
15.2Deconstructingtypes. . . . . . . . . . . . . . . . . . . . . . . . . . . 106
15.3
type
formakingtypesynonyms
. . . . . . . . . . . . . . . . . . . . . 108
16 Pattern matching
111
16.1Analysingpatternmatching
. . . . . . . . . . . . . . . . . . . . . . . . 111
16.2Theconnectionwithconstructors. . . . . . . . . . . . . . . . . . 112
16.3Matchingliteralvalues
. . . . . . . . . . . . . . . . . . . . . . . . . 114
16.4Syntaxtricks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
16.5Wherewecanuseit
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
IV
Contents
17 Control structures 119
17.1
if
andguardsrevisited. . . . . . . . . . . . . . . . . . . . . . . . . . 119
17.2
case
expressions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
17.3Controllingactions,revisited. . . . . . . . . . . . . . . . . . . . . 122
18 More on functions 125
18.1letandwhererevisited. . . . . . . . . . . . . . . . . . . . . . . . . 125
18.2AnonymousFunctions-lambdas
. . . . . . . . . . . . . . . . . . . . 126
18.3Operatorsandsections
. . . . . . . . . . . . . . . . . . . . . . . . . . 127
19 Higher order functions and Currying
129
19.1TheQuickestSortingAlgorithmInTown
. . . . . . . . . . . . . 129
19.2Now,HowDoWeUseIt? . . . . . . . . . . . . . . . . . . . . . . . . 130
19.3TweakingWhatWeAlreadyHave
. . . . . . . . . . . . . . . . . . 130
19.4
quickSort
,TakeTwo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
19.5ButWhatDidWeGain?
. . . . . . . . . . . . . . . . . . . . . . . . . . 131
19.6Higher-OrderFunctionsandTypes. . . . . . . . . . . . . . . . . . 132
19.7Currying
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
19.8Functionmanipulation. . . . . . . . . . . . . . . . . . . . . . . . . . 134
20 Using GHCi eectively 137
20.1Userinterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
21 Intermediate Haskell
139
22 Modules
141
22.1Modules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
22.2Importing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
22.3Exporting
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
23 Indentation
145
23.1Thegoldenruleofindentation
. . . . . . . . . . . . . . . . . . . . 145
23.2Amechanicaltranslation
. . . . . . . . . . . . . . . . . . . . . . . . 146
23.3Layoutinaction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
24 More on datatypes
151
24.1Enumerations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
24.2NamedFields(RecordSyntax)
. . . . . . . . . . . . . . . . . . . . . . 151
24.3ParameterizedTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
25 Other data structures 157
25.1Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
25.2Otherdatatypes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
26 Classes and types
167
26.1Classesandinstances
. . . . . . . . . . . . . . . . . . . . . . . . . . . 167
26.2Deriving. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
26.3Classinheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
26.4Standardclasses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
V
Plik z chomika:
aiforever
Inne pliki z tego folderu:
[Donald_E._Knuth]_Literate_Programming(BookFi.org).djvu
(2858 KB)
getting started with erlang.pdf
(185 KB)
Haskell.pdf
(3831 KB)
Introduction to objective caml.pdf
(2285 KB)
Java. Tablice Informatyczne.pdf
(9111 KB)
Inne foldery tego chomika:
FIZYKA
KOMPRESJA
KRYPTOGRAFIA
MATEMATYKA
OXFORD PRESS
Zgłoś jeśli
naruszono regulamin