Haskell: Notes for Professionals

GoalKicker.com (CC BY-SA)
Page 1 sur 230Lecteur de document UniversityLib

Haskell: Notes for Professionals

FST · Programming, Haskell · notes

Voir tous les documents en programmation

Haskell

Notes for Professionals

Haskell

Notes for Professionals

200+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an uno$cial free book created for educational purposes and is

not a$liated with o$cial Haskell group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with Haskell Language

.....................................................................................

2

............................................................................................................................................

...............................................................................................................................................

......................................................................................................................................................

............................................................................................................

6

Section 1.1: Getting started

Section 1.2: Hello, World!

Section 1.3: Factorial

Section 1.4: Fibonacci, Using Lazy Evaluation

Section 1.5: Primes

Section 1.6: Declaring Values

Chapter 2: Overloaded Literals

.........................................................................................................................................................

........................................................................................................................................

...........................................................................................................................

.......................................................................................................................................................

....................................................................................................................................

10

......................................................................................................................................

..............................................................................................................................................

Section 2.1: Strings

Section 2.2: Floating Numeral

Section 2.3: Integer Numeral

Section 2.4: List Literals

Chapter 3: Foldable

...................................................................................................................................................

13

..............................................................................................................................

Section 3.1: Denition of Foldable

Section 3.2: An instance of Foldable for a binary tree

Section 3.3: Counting the elements of a Foldable structure

Section 3.4: Folding a structure in reverse

Section 3.5: Flattening a Foldable structure into a list

Section 3.6: Performing a side-e#ect for each element of a Foldable structure

Section 3.7: Flattening a Foldable structure into a Monoid

Section 3.8: Checking if a Foldable structure is empty

............................................................................................

...................................................................................

...............................................................................................................

............................................................................................

.................................................

15

....................................................................................

...........................................................................................

Chapter 4: Traversable

...........................................................................................................................................

18

........................................................................................................................

Section 4.1: Denition of Traversable

Section 4.2: Traversing a structure in reverse

Section 4.3: An instance of Traversable for a binary tree

Section 4.4: Traversable structures as shapes with contents

Section 4.5: Instantiating Functor and Foldable for a Traversable structure

Section 4.6: Transforming a Traversable structure with the aid of an accumulating parameter

Section 4.7: Transposing a list of lists

......................................................................................

................................................................................

.........................................................................................................

.......................................................................................................................

22

.......................................................

......................

21

Chapter 5: Lens

............................................................................................................................................................

24

Section 5.1: Lenses for records

Section 5.2: Manipulating tuples with Lens

Section 5.3: Lens and Prism

Section 5.4: Stateful Lenses

Section 5.5: Lenses compose

Section 5.6: Writing a lens without Template Haskell

Section 5.7: Fields with makeFields

Section 5.8: Classy Lenses

Section 5.9: Traversals

...................................................................................................................................

...............................................................................................................

........................................................................................................................................

........................................................................................................................................

.....................................................................................................................................

.............................................................................................

26

...........................................................................................................................

..........................................................................................................................................

................................................................................................................................................

Chapter 6: QuickCheck

.............................................................................................................................................

30

...............................................................................................................................

Section 6.1: Declaring a property

Section 6.2: Randomly generating data for custom types

Section 6.3: Using implication (==>) to check properties with preconditions

Section 6.4: Checking a single property

Section 6.5: Checking all the properties in a le

Section 6.6: Limiting the size of test data

...................................................................................................................

......................................................................................................

.................................................................................................................

.....................................................................................

........................................................

30

2

4

6

7

8

10

10

11

11

13

13

Publicité

14

14

15

16

16

18

18

19

20

20

24

24

25

25

26

27

29

29

30

30

30

31

31

Chapter 7: Common GHC Language Extensions

.........................................................................................

33

Section 7.1: RankNTypes

Section 7.2: OverloadedStrings

Section 7.3: BinaryLiterals

Section 7.4: ExistentialQuantication

Section 7.5: LambdaCase

Section 7.6: FunctionalDependencies

Section 7.7: FlexibleInstances

Section 7.8: GADTs

Section 7.9: TupleSections

Section 7.10: OverloadedLists

Section 7.11: MultiParamTypeClasses

Section 7.12: UnicodeSyntax

Section 7.13: PatternSynonyms

Section 7.14: ScopedTypeVariables

Section 7.15: RecordWildCards

.............................................................................................................................................

..................................................................................................................................

..........................................................................................................................................

........................................................................................................................

...........................................................................................................................................

........................................................................................................................

36

.....................................................................................................................................

......................................................................................................................................................

..........................................................................................................................................

.....................................................................................................................................

........................................................................................................................

.......................................................................................................................................

..................................................................................................................................

...........................................................................................................................

...................................................................................................................................

Chapter 8: Free Monads

..........................................................................................................................................

42

Section 8.1: Free monads split monadic computations into data structures and interpreters

Section 8.2: The Freer monad

Section 8.3: How do foldFree and iterM work?

Section 8.4: Free Monads are like xed points

....................................................................................................................................

.........................................................................................................

........................................................................................................

...........................

42

Chapter 9: Type Classes

..........................................................................................................................................

46

..............................................................................................................................................................

.....................................................................................................................................................

...........................................................................................................................................................

..........................................................................................................................................................

Section 9.1: Eq

Section 9.2: Monoid

Section 9.3: Ord

Section 9.4: Num

Section 9.5: Maybe and the Functor Class

Section 9.6: Type class inheritance: Ord type class

...............................................................................................................

.................................................................................................

49

Chapter 10: IO

...............................................................................................................................................................

51

..................................................................................................................

............................................................................................

.....................................................................................................

........................................................................

Section 10.1: Getting the 'a' "out of" 'IO a'

Section 10.2: IO denes your program's main action

Section 10.3: Checking for end-of-le conditions

Section 10.4: Reading all contents of standard input into a string

Section 10.5: Role and Purpose of IO

Section 10.6: Writing to stdout

Section 10.7: Reading words from an entire le

Section 10.8: Reading a line from standard input

Section 10.9: Reading from stdin

Section 10.10: Parsing and constructing an object from standard input

Section 10.11: Reading from le handles

........................................................................................................................

....................................................................................................................................

.......................................................................................................

....................................................................................................

..............................................................................................................................

...................................................................................................................

...............................................................

57

.....................................................................................................................................

............................................................................................................................................

..................................................................................................

...................................................................................................................................

Chapter 11: Record Syntax

Section 11.1: Basic Syntax

Section 11.2: Dening a data type with eld labels

Section 11.3: RecordWildCards

Section 11.4: Copying Records while Changing Field Values

Section 11.5: Records with newtype

Chapter 12: Partial Application

...........................................................................................................................

............................................................................................................................

...................................................................................

61

Section 12.1: Sections

Section 12.2: Partially Applied Adding Function

Section 12.3: Returning a Partially Applied Function

.......................................................................................................

...............................................................................................

64

Publicité

...................................................................................................................................................

33

33

34

34

35

36

37

37

38

38

39

39

40

41

43

44

45

46

46

47

47

49

51

51

52

52

53

55

56

56

57

58

59

59

60

60

61

63

63

63

Chapter 13: Monoid

.....................................................................................................................................................

65

Section 13.1: An instance of Monoid for lists

Section 13.2: Collapsing a list of Monoids into a single value

Section 13.3: Numeric Monoids

Section 13.4: An instance of Monoid for ()

...................................................................................................................................

................................................................................................................

..............................................................................................................

65

.................................................................................

65

Chapter 14: Category Theory

..............................................................................................................................

67

Section 14.1: Category theory as a system for organizing abstraction

Section 14.2: Haskell types as a category

Section 14.3: Denition of a Category

Section 14.4: Coproduct of types in Hask

Section 14.5: Product of types in Hask

Section 14.6: Haskell Applicative in terms of Category Theory

................................................................................................................

.......................................................................................................................

.................................................................................................................

......................................................................................................................

..............................................................................

.................................................................

67

Chapter 15: Lists

..........................................................................................................................................................

73

................................................................................................................................................

......................................................................................................................................

....................................................................................................................................................

.............................................................................................................................................

................................................................................................................................

...................................................................................................................

........................................................................................................................

......................................................................................................................

................................................................................................................................

Section 15.1: List basics

Section 15.2: Processing lists

Section 15.3: Ranges

Section 15.4: List Literals

Section 15.5: List Concatenation

Section 15.6: Accessing elements in lists

Section 15.7: Basic Functions on Lists

Section 15.8: Transforming with map

Section 15.9: Filtering with lter

Section 15.10: foldr

Section 15.11: Zipping and Unzipping Lists

Section 15.12: foldl

.......................................................................................................................................................

........................................................................................................................................................

78

.................................................................................................................

77

Chapter 16: Sorting Algorithms

............................................................................................................................

79

Section 16.1: Insertion Sort

Section 16.2: Permutation Sort

Section 16.3: Merge Sort

Section 16.4: Quicksort

Section 16.5: Bubble sort

Section 16.6: Selection sort

Chapter 17: Type Families

...........................................................................................................................................

79

...................................................................................................................................

79

..............................................................................................................................................

................................................................................................................................................

.............................................................................................................................................

.........................................................................................................................................

Section 17.1: Datatype Families

Section 17.2: Type Synonym Families

Section 17.3: Injectivity

......................................................................................................................................

..................................................................................................................................

.......................................................................................................................

81

.................................................................................................................................................

83

Chapter 18: Monads

...................................................................................................................................................

84

...............................................................................................................................

84

Section 18.1: Denition of Monad

Publicité

Section 18.2: No general way to extract value from a monadic computation

Section 18.3: Monad as a Subclass of Applicative

Section 18.4: The Maybe monad

Section 18.5: IO monad

Section 18.6: List Monad

Section 18.7: do-notation

...............................................................................................................................................

..............................................................................................................................................

............................................................................................................................................

................................................................................................................................

....................................................................................................

.....................................................

84

Chapter 19: Stack

........................................................................................................................................................

90

Section 19.1: Proling with Stack

Section 19.2: Structure

Section 19.3: Build and Run a Stack Project

Section 19.4: Viewing dependencies

.................................................................................................................................

.................................................................................................................................................

..............................................................................................................

90

..........................................................................................................................

90

65

66

67

69

70

71

72

73

73

74

75

75

75

75

76

76

77

79

80

80

80

81

81

85

85

87

88

88

90

90

Section 19.5: Stack install

Section 19.6: Installing Stack

Section 19.7: Creating a simple project

Section 19.8: Stackage Packages and changing the LTS (resolver) version

............................................................................................................................................

.......................................................................................................................................

.....................................................................................................................

........................................................

91

Chapter 20: Generalized Algebraic Data Types

.........................................................................................

93

Section 20.1: Basic Usage

...........................................................................................................................................

93

Chapter 21: Recursion Schemes

..........................................................................................................................

94

.............................................................................................................................................

.................................................................................................................................

.............................................................................................................................

Section 21.1: Fixed points

Section 21.2: Primitive recursion

Section 21.3: Primitive corecursion

Section 21.4: Folding up a structure one layer at a time

Section 21.5: Unfolding a structure one layer at a time

Section 21.6: Unfolding and then folding, fused

.........................................................................................

95

..........................................................................................

.......................................................................................................

Chapter 22: Data.Text

..............................................................................................................................................

97

............................................................................................................................................

97

Section 22.1: Text Literals

Section 22.2: Checking if a Text is a substring of another Text

Section 22.3: Stripping whitespace

Section 22.4: Indexing Text

Section 22.5: Splitting Text Values

Section 22.6: Encoding and Decoding Text

............................................................................................................................

.........................................................................................................................................

.............................................................................................................................

..............................................................................................................

.............................................................................

97

Chapter 23: Using GHCi

..........................................................................................................................................

100

Section 23.1: Breakpoints with GHCi

Section 23.2: Quitting GHCi

Section 23.3: Reloading a already loaded le

Section 23.4: Starting GHCi

Section 23.5: Changing the GHCi default prompt

Section 23.6: The GHCi conguration le

Section 23.7: Loading a le

Section 23.8: Multi-line statements

........................................................................................................................

......................................................................................................................................

........................................................................................................

......................................................................................................................................

..................................................................................................

101

...............................................................................................................

......................................................................................................................................

..........................................................................................................................

Chapter 24: Strictness

...........................................................................................................................................

103

Section 24.1: Bang Patterns

Section 24.2: Lazy patterns

Section 24.3: Normal forms

Section 24.4: Strict elds

......................................................................................................................................

Publicité

......................................................................................................................................

......................................................................................................................................

104

...........................................................................................................................................

Chapter 25: Syntax in Functions

Section 25.1: Pattern Matching

Section 25.2: Using where and guards

Section 25.3: Guards

Chapter 26: Functor

.......................................................................................................................

.................................................................................................................................

...................................................................................................................

106

.................................................................................................................................................

.................................................................................................................................................

Section 26.1: Class Denition of Functor and Laws

Section 26.2: Replacing all elements of a Functor with a single value

Section 26.3: Common instances of Functor

Section 26.4: Deriving Functor

Section 26.5: Polynomial functors

Section 26.6: Functors in Category Theory

.................................................................................................................................

...........................................................................................................................

............................................................................................................

..........................................................................................................

...............................................................................................

................................................................

108

Chapter 27: Testing with Tasty

.........................................................................................................................

114

Section 27.1: SmallCheck, QuickCheck and HUnit

Chapter 28: Creating Custom Data Types

..................................................................................................

114

..................................................................................................

115

Section 28.1: Creating a data type with value constructor parameters

..............................................................

115

91

91

91

94

94

95

95

95

97

98

98

99

100

100

101

101

101

102

102

103

103

105

106

106

107

108

108

108

110

111

112

Section 28.2: Creating a data type with type parameters

Section 28.3: Creating a simple data type

Section 28.4: Custom data type with record parameters

.............................................................................................................

.....................................................................................

...................................................................................

115

Chapter 29: Reactive-banana

............................................................................................................................

117

..........................................................................................

117

Section 29.1: Injecting external events into the library

Section 29.2: Event type

Section 29.3: Actuating EventNetworks

Section 29.4: Behavior type

Chapter 30: Optimization

Section 30.1: Compiling your Program for Proling

Section 30.2: Cost Centers

Chapter 31: Concurrency

...........................................................................................................................................

..................................................................................................................

.....................................................................................................................................

.....................................................................................................................................

..............................................................................................

119

........................................................................................................................................

.......................................................................................................................................

Section 31.1: Spawning Threads with forkIO

Section 31.2: Communicating between Threads with MVar

Section 31.3: Atomic Blocks with Software Transactional Memory

................................................................................

.....................................................................

122

..........................................................................................................

Chapter 32: Function composition

Section 32.1: Right-to-left composition

Section 32.2: Composition with binary function

Section 32.3: Left-to-right composition

...................................................................................................................

...................................................................................................................

.....................................................................................................

124

...................................................................................................................

124

Chapter 33: Databases

Section 33.1: Postgres

..........................................................................................................................................

125

................................................................................................................................................

125

Chapter 34: Data.Aeson - JSON in Haskell

.................................................................................................

126

Section 34.1: Smart Encoding and Decoding using Generics

Section 34.2: A quick way to generate a Data.Aeson.Value

Section 34.3: Optional Fields

................