HaskellNotesForProfessionals.pdf

(1943 KB) Pobierz
Notes for Professionals
Haskell
Haskell
Notes for Professionals
of professional hints and tricks
200+ pages
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
Section 1.1: Getting started
............................................................................................................................................ 2
Section 1.2: Hello, World!
............................................................................................................................................... 4
Section 1.3: Factorial
...................................................................................................................................................... 6
Section 1.4: Fibonacci, Using Lazy Evaluation
............................................................................................................ 6
Section 1.5: Primes
......................................................................................................................................................... 7
Section 1.6: Declaring Values
........................................................................................................................................ 8
Chapter 2: Overloaded Literals
........................................................................................................................... 10
Section 2.1: Strings
....................................................................................................................................................... 10
Section 2.2: Floating Numeral
.................................................................................................................................... 10
Section 2.3: Integer Numeral
...................................................................................................................................... 11
Section 2.4: List Literals
.............................................................................................................................................. 11
Chapter 3: Foldable
................................................................................................................................................... 13
Section 3.1: Definition of Foldable
.............................................................................................................................. 13
Section 3.2: An instance of Foldable for a binary tree
............................................................................................ 13
Section 3.3: Counting the elements of a Foldable structure
................................................................................... 14
Section 3.4: Folding a structure in reverse
............................................................................................................... 14
Section 3.5: Flattening a Foldable structure into a list
............................................................................................ 15
Section 3.6: Performing a side-eect for each element of a Foldable structure
................................................. 15
Section 3.7: Flattening a Foldable structure into a Monoid
.................................................................................... 16
Section 3.8: Checking if a Foldable structure is empty
........................................................................................... 16
Chapter 4: Traversable
........................................................................................................................................... 18
Section 4.1: Definition of Traversable
........................................................................................................................ 18
Section 4.2: Traversing a structure in reverse
......................................................................................................... 18
Section 4.3: An instance of Traversable for a binary tree
...................................................................................... 19
Section 4.4: Traversable structures as shapes with contents
................................................................................ 20
Section 4.5: Instantiating Functor and Foldable for a Traversable structure
....................................................... 20
Section 4.6: Transforming a Traversable structure with the aid of an accumulating parameter
...................... 21
Section 4.7: Transposing a list of lists
....................................................................................................................... 22
Chapter 5: Lens
............................................................................................................................................................ 24
Section 5.1: Lenses for records
................................................................................................................................... 24
Section 5.2: Manipulating tuples with Lens
............................................................................................................... 24
Section 5.3: Lens and Prism
........................................................................................................................................ 25
Section 5.4: Stateful Lenses
........................................................................................................................................ 25
Section 5.5: Lenses compose
..................................................................................................................................... 26
Section 5.6: Writing a lens without Template Haskell
............................................................................................. 26
Section 5.7: Fields with makeFields
........................................................................................................................... 27
Section 5.8: Classy Lenses
.......................................................................................................................................... 29
Section 5.9: Traversals
................................................................................................................................................ 29
Chapter 6: QuickCheck
............................................................................................................................................. 30
Section 6.1: Declaring a property
............................................................................................................................... 30
Section 6.2: Randomly generating data for custom types
..................................................................................... 30
Section 6.3: Using implication (==>) to check properties with preconditions
........................................................ 30
Section 6.4: Checking a single property
................................................................................................................... 30
Section 6.5: Checking all the properties in a file
...................................................................................................... 31
Section 6.6: Limiting the size of test data
................................................................................................................. 31
Chapter 7: Common GHC Language Extensions
......................................................................................... 33
Section 7.1: RankNTypes
............................................................................................................................................. 33
Section 7.2: OverloadedStrings
.................................................................................................................................. 33
Section 7.3: BinaryLiterals
.......................................................................................................................................... 34
Section 7.4: ExistentialQuantification
........................................................................................................................ 34
Section 7.5: LambdaCase
........................................................................................................................................... 35
Section 7.6: FunctionalDependencies
........................................................................................................................ 36
Section 7.7: FlexibleInstances
..................................................................................................................................... 36
Section 7.8: GADTs
...................................................................................................................................................... 37
Section 7.9: TupleSections
.......................................................................................................................................... 37
Section 7.10: OverloadedLists
..................................................................................................................................... 38
Section 7.11: MultiParamTypeClasses
........................................................................................................................ 38
Section 7.12: UnicodeSyntax
....................................................................................................................................... 39
Section 7.13: PatternSynonyms
.................................................................................................................................. 39
Section 7.14: ScopedTypeVariables
........................................................................................................................... 40
Section 7.15: RecordWildCards
................................................................................................................................... 41
Chapter 8: Free Monads
.......................................................................................................................................... 42
Section 8.1: Free monads split monadic computations into data structures and interpreters
........................... 42
Section 8.2: The Freer monad
.................................................................................................................................... 43
Section 8.3: How do foldFree and iterM work?
........................................................................................................ 44
Section 8.4: Free Monads are like fixed points
......................................................................................................... 45
Chapter 9: Type Classes
.......................................................................................................................................... 46
Section 9.1: Eq
.............................................................................................................................................................. 46
Section 9.2: Monoid
..................................................................................................................................................... 46
Section 9.3: Ord
........................................................................................................................................................... 47
Section 9.4: Num
.......................................................................................................................................................... 47
Section 9.5: Maybe and the Functor Class
............................................................................................................... 49
Section 9.6: Type class inheritance: Ord type class
................................................................................................. 49
Chapter 10: IO
............................................................................................................................................................... 51
Section 10.1: Getting the 'a' "out of" 'IO a'
.................................................................................................................. 51
Section 10.2: IO defines your program's `main` action
............................................................................................ 51
Section 10.3: Checking for end-of-file conditions
..................................................................................................... 52
Section 10.4: Reading all contents of standard input into a string
........................................................................ 52
Section 10.5: Role and Purpose of IO
........................................................................................................................ 53
Section 10.6: Writing to stdout
.................................................................................................................................... 55
Section 10.7: Reading words from an entire file
....................................................................................................... 56
Section 10.8: Reading a line from standard input
.................................................................................................... 56
Section 10.9: Reading from `stdin`
.............................................................................................................................. 57
Section 10.10: Parsing and constructing an object from standard input
............................................................... 57
Section 10.11: Reading from file handles
................................................................................................................... 58
Chapter 11: Record Syntax
..................................................................................................................................... 59
Section 11.1: Basic Syntax
............................................................................................................................................ 59
Section 11.2: Defining a data type with field labels
.................................................................................................. 60
Section 11.3: RecordWildCards
................................................................................................................................... 60
Section 11.4: Copying Records while Changing Field Values
................................................................................... 61
Section 11.5: Records with newtype
........................................................................................................................... 61
Chapter 12: Partial Application
............................................................................................................................ 63
Section 12.1: Sections
................................................................................................................................................... 63
Section 12.2: Partially Applied Adding Function
....................................................................................................... 63
Section 12.3: Returning a Partially Applied Function
............................................................................................... 64
Chapter 13: Monoid
..................................................................................................................................................... 65
Section 13.1: An instance of Monoid for lists
.............................................................................................................. 65
Section 13.2: Collapsing a list of Monoids into a single value
................................................................................. 65
Section 13.3: Numeric Monoids
................................................................................................................................... 65
Section 13.4: An instance of Monoid for ()
................................................................................................................ 66
Chapter 14: Category Theory
.............................................................................................................................. 67
Section 14.1: Category theory as a system for organizing abstraction
................................................................. 67
Section 14.2: Haskell types as a category
................................................................................................................ 67
Section 14.3: Definition of a Category
....................................................................................................................... 69
Section 14.4: Coproduct of types in Hask
................................................................................................................. 70
Section 14.5: Product of types in Hask
...................................................................................................................... 71
Section 14.6: Haskell Applicative in terms of Category Theory
.............................................................................. 72
Chapter 15: Lists
.......................................................................................................................................................... 73
Section 15.1: List basics
................................................................................................................................................ 73
Section 15.2: Processing lists
...................................................................................................................................... 73
Section 15.3: Ranges
.................................................................................................................................................... 74
Section 15.4: List Literals
............................................................................................................................................. 75
Section 15.5: List Concatenation
................................................................................................................................ 75
Section 15.6: Accessing elements in lists
................................................................................................................... 75
Section 15.7: Basic Functions on Lists
........................................................................................................................ 75
Section 15.8: Transforming with `map`
...................................................................................................................... 76
Section 15.9: Filtering with `filter`
................................................................................................................................ 76
Section 15.10: foldr
....................................................................................................................................................... 77
Section 15.11: Zipping and Unzipping Lists
................................................................................................................. 77
Section 15.12: foldl
........................................................................................................................................................ 78
Chapter 16: Sorting Algorithms
............................................................................................................................ 79
Section 16.1: Insertion Sort
........................................................................................................................................... 79
Section 16.2: Permutation Sort
................................................................................................................................... 79
Section 16.3: Merge Sort
.............................................................................................................................................. 79
Section 16.4: Quicksort
................................................................................................................................................ 80
Section 16.5: Bubble sort
............................................................................................................................................. 80
Section 16.6: Selection sort
......................................................................................................................................... 80
Chapter 17: Type Families
...................................................................................................................................... 81
Section 17.1: Datatype Families
.................................................................................................................................. 81
Section 17.2: Type Synonym Families
....................................................................................................................... 81
Section 17.3: Injectivity
................................................................................................................................................. 83
Chapter 18: Monads
................................................................................................................................................... 84
Section 18.1: Definition of Monad
............................................................................................................................... 84
Section 18.2: No general way to extract value from a monadic computation
..................................................... 84
Section 18.3: Monad as a Subclass of Applicative
.................................................................................................... 85
Section 18.4: The Maybe monad
................................................................................................................................ 85
Section 18.5: IO monad
............................................................................................................................................... 87
Section 18.6: List Monad
.............................................................................................................................................. 88
Section 18.7: do-notation
............................................................................................................................................ 88
Chapter 19: Stack
........................................................................................................................................................ 90
Section 19.1: Profiling with Stack
................................................................................................................................. 90
Section 19.2: Structure
................................................................................................................................................. 90
Section 19.3: Build and Run a Stack Project
.............................................................................................................. 90
Section 19.4: Viewing dependencies
.......................................................................................................................... 90
Section 19.5: Stack install
............................................................................................................................................ 91
Section 19.6: Installing Stack
....................................................................................................................................... 91
Section 19.7: Creating a simple project
..................................................................................................................... 91
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
............................................................................................................................................. 94
Section 21.2: Primitive recursion
................................................................................................................................. 94
Section 21.3: Primitive corecursion
............................................................................................................................. 95
Section 21.4: Folding up a structure one layer at a time
......................................................................................... 95
Section 21.5: Unfolding a structure one layer at a time
.......................................................................................... 95
Section 21.6: Unfolding and then folding, fused
....................................................................................................... 95
Chapter 22: Data.Text
.............................................................................................................................................. 97
Section 22.1: Text Literals
............................................................................................................................................ 97
Section 22.2: Checking if a Text is a substring of another Text
............................................................................. 97
Section 22.3: Stripping whitespace
............................................................................................................................ 97
Section 22.4: Indexing Text
......................................................................................................................................... 98
Section 22.5: Splitting Text Values
............................................................................................................................. 98
Section 22.6: Encoding and Decoding Text
.............................................................................................................. 99
Chapter 23: Using GHCi
.......................................................................................................................................... 100
Section 23.1: Breakpoints with GHCi
........................................................................................................................ 100
Section 23.2: Quitting GHCi
...................................................................................................................................... 100
Section 23.3: Reloading a already loaded file
........................................................................................................ 101
Section 23.4: Starting GHCi
...................................................................................................................................... 101
Section 23.5: Changing the GHCi default prompt
.................................................................................................. 101
Section 23.6: The GHCi configuration file
............................................................................................................... 101
Section 23.7: Loading a file
...................................................................................................................................... 102
Section 23.8: Multi-line statements
.......................................................................................................................... 102
Chapter 24: Strictness
........................................................................................................................................... 103
Section 24.1: Bang Patterns
...................................................................................................................................... 103
Section 24.2: Lazy patterns
...................................................................................................................................... 103
Section 24.3: Normal forms
...................................................................................................................................... 104
Section 24.4: Strict fields
........................................................................................................................................... 105
Chapter 25: Syntax in Functions
....................................................................................................................... 106
Section 25.1: Pattern Matching
................................................................................................................................. 106
Section 25.2: Using where and guards
................................................................................................................... 106
Section 25.3: Guards
................................................................................................................................................. 107
Chapter 26: Functor
................................................................................................................................................. 108
Section 26.1: Class Definition of Functor and Laws
............................................................................................... 108
Section 26.2: Replacing all elements of a Functor with a single value
................................................................ 108
Section 26.3: Common instances of Functor
.......................................................................................................... 108
Section 26.4: Deriving Functor
................................................................................................................................. 110
Section 26.5: Polynomial functors
........................................................................................................................... 111
Section 26.6: Functors in Category Theory
............................................................................................................ 112
Chapter 27: Testing with Tasty
......................................................................................................................... 114
Section 27.1: SmallCheck, QuickCheck and HUnit
.................................................................................................. 114
Chapter 28: Creating Custom Data Types
.................................................................................................. 115
Section 28.1: Creating a data type with value constructor parameters
.............................................................. 115
Zgłoś jeśli naruszono regulamin