Haskell Language
Total Page:16
File Type:pdf, Size:1020Kb
Haskell Language #haskell 1 1: Haskell 2 2 2 2 Examples 2 2 3 4 1 4 2 4 Fibonacci 4 5 REPL 5 GHCI 5 7 7 100 7 7 7 7 7 7 7 2: Attoparsec 9 9 9 Examples 9 9 - 10 3: Bifunctor 11 11 11 Examples 11 Bifunctor 11 11 Either 11 11 Bifunctor 12 4: Data.Aeson - HaskellJSON 13 Examples 13 13 Data.Aeson.Value 13 14 5: Data.Text 15 15 Examples 15 15 15 16 16 TextText 16 17 6: GHCJS 18 18 Examples 18 “Hello World”Node.js 18 7: Google Protocol Buffers 19 19 Examples 19 .proto 19 8: Gtk3 21 21 21 Examples 21 GtkHello World 21 9: IO 22 Examples 22 22 22 22 22 23 24 IO“main” 24 IO 25 IO 25 IO 26 IO 26 IOdo 26 'a'“”'IO a' 27 stdout 27 putChar :: Char -> IO () - charstdout 27 putStr :: String -> IO () - Stringstdout 27 putStrLn :: String -> IO () - Stringstdout 27 print :: Show a => a -> IO () - a Showstdout 27 `stdin` 28 getChar :: IO Char - stdinChar 28 getLine :: IO String - stdinString 28 read :: Read a => String -> a - String 28 10: Monad 30 Examples 30 monadic 30 30 31 32 11: Profunctor 33 33 33 33 Examples 33 - >Profunctor 33 12: Web 34 Examples 34 34 35 13: XML 36 36 Examples 36 `xml` 36 14: zipWithM 37 37 37 Examples 37 37 15: 38 38 Examples 38 `forkIO` 38 “MVar” 38 39 atomically :: STM a -> IO a 39 readTVar :: TVar a -> STM a 39 writeTVar :: TVar a -> a -> STM () 39 16: 41 41 Examples 41 41 41 41 41 41 42 42 17: 43 Examples 43 Monoid 43 Monoids 43 43 Monoid 44 18: 45 Examples 45 45 “” 45 45 19: RankNTypes 47 47 47 Examples 47 RankNTypes 47 20: GHCi 48 48 Examples 48 GHCi 48 GHCi 48 GHCi 48 48 GHCi 49 49 GHCi 49 50 21: taggy-lensHTML 51 Examples 51 iddiv 51 51 22: 53 Examples 53 53 53 23: ...... 54 54 Examples 54 54 54 55 55 uncurrying 55 currying 56 56 56 24: Monads 57 Examples 57 monadmonadic 57 Monads 58 foldFreeiterM 58 monad 59 25: 61 61 61 61 61 Examples 61 Functor 61 61 62 62 63 Functor 63 63 63 64 Functor 64 Functor 64 Functor 65 65 65 66 26: 67 Examples 67 67 67 68 27: 69 69 69 Examples 69 69 69 - 1 69 - 2 70 28: 71 Examples 71 71 71 71 71 Haskell 72 72 72 72 73 Hask 73 73 73 74 74 Hask 74 74 74 Hask 74 Haskell 75 29: 76 Examples 76 76 76 76 77 77 77 77 30: 78 Examples 78 78 78 78 78 78 78 78 31: 80 80 Examples 80 80 81 81 32: 82 Examples 82 82 82 83 EventNetworks 83 33: 85 85 85 Examples 85 85 85 85 86 87 Monoid 88 88 88 34: 90 90 Examples 90 monad 90 IO monad 92 Monad 92 Monad 93 monadic 93 94 Monad 94 35: 96 Examples 96 96 96 96 96 96 97 36: 99 99 99 99 Examples 99 99 100 100 100 37: Monad 101 101 101 Examples 101 101 101 102 postIncrement 102 102 Traversable 102 Traversable 102 38: 104 Examples 104 104 104 104 104 104 StackageLTS 104 104 105 105 105 105 39: 107 107 107 Examples 107 HaskellC 107 HaskellC 108 40: - Data.Map 109 Examples 109 109 109 109 109 110 110 Monoid 110 41: 111 Examples 111 111 111 42: GHC 113 113 Examples 113 MultiParamTypeClasses 113 FlexibleInstances 113 OverloadedStrings 113 TupleSections 114 N 114 114 UnicodeSyntax 114 BinaryLiterals 115 ExistentialQuantification 115 LambdaCase 116 RankNTypes 117 OverloadedLists 117 FunctionalDependencies 118 GADTs 118 ScopedTypeVariables 119 PatternSynonyms 119 RecordWildCards 120 43: monadmonad 121 Examples 121 ~~ 121 ~~Nat~~Nat 121 ~~ MaybeTNat 121 w~~[W] 122 Const c~~ c 122 x~~x 122 44: cofree comonads 124 Examples 124 Cofree~~ 124 CofreeConst c~~c 124 Cofree~~ 124 Cofree~~ NonEmpty 124 Cofreew~~ WriterT w Stream 124 CofreeE e~~ NonEmptyTe 125 Cofreex~~ Moore x 125 45: 126 Examples 126 126 46: 127 Examples 127 127 47: 128 Examples 128 128 128 128 129 ==> 129 129 48: 130 130 130 130 Examples 130 130 130 130 130 131 131 49: 133 133 Examples 133 133 133 133 133 50: 135 Examples 135 135 135 135 136 136 136 51: 137 137 137 Examples 137 Eval Monad 137 RPAR 137 RSEQ 138 52: 140 Examples 140 Postgres 140 140 140 53: 141 141 141 Examples 141 141 141 54: 143 143 143 Examples 143 143 143 144 144 144 144 55: HaskellQuasiQuotes 146 146 Haskell 146 146 Haskell 146 Examples 146 Q 146 147 HaskellQuasiquotes 148 148 QuasiQuotation 148 149 QuasiQuotes 149 149 56: IO 150 Examples 150 IO 150 57: 151 151 151 Examples 152 152 152 152 152 153 153 154 foldl 154 foldr 155 `map` 155 `' 155 156 58: 157 Examples 157 SmallCheckQuickCheckHUnit 157 59: 158 158 Examples 158 Data.Vector 158 158 `map`Reducing`fold` 158 159 60: 160 160 Examples 160 FunctorFoldable 160 Traversable 160 162 162 162 163 164 61: 166 166 166 166 Examples 167 167 `Fractional Int...` 168 168 62: 169 169 Examples 169 169 169 169 runEffect 170 170 monad 170 170 63: 173 Examples 173 173 64: 174 174 174 Examples 174 174 174 174 65: 175 175 Examples 175 hslogger 175 66: 176 Examples 176 176 176 newtype 177 RecordWildCards 177 177 178 NamedFieldPuns 178 RecordWildcards 178 178 67: / 179 179 Examples 179 179 68: 180 180 Examples 180 180 180 181 181 69: 182 182 Examples 182 182 182 183 183 183 183 70: 184 184 Examples 184 184 184 185 185 71: GHC 186 Examples 186 186 72: 187 187 187 187 187 187 Examples 187 187 187 187 188 188 188 188 188 188 189 73: 190 Examples 190 190 190 190 190 191 191 191 191 74: 192 192 192 Examples 192 Functor Class 192 Ord 193 193 193 193 193 193 194 194 194 194 194 194 194 194 194 75: 196 196 196 196 196 196 196 196 Examples 197 197 197 197 197 197 & 197 198 Haskell 198 199 199 200 200 makeFields 200 76: 202 202 Examples 203 203 203 77: 204 Examples 204 204 204 204 204 204 205 205 205 206 206 206 78: 207 207 Examples 207 207 Lambda 207 208 209 You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: haskell-language It is an unofficial and free Haskell Language ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Haskell Language. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/zh-TW/home 1 1: Haskell Haskell。 • Haskell。。。 • Haskell。。 • ConcurrentGHC。 • 。。 • Haskell。 • Haskell。 HaskellHaskell 2010.20165Haskell 2020。 Haskell。。 Haskell 2010 2012-07-10 98 2002-12-01 Examples “” Haskell main :: IO () main = putStrLn "Hello, World!" mainIO ()“”type ()I / O“unit”;。 main。 helloworld.hsHaskellGHC ghc helloworld.hs "Hello, World!" ./helloworld Hello, World! runhaskellrunghc runhaskell helloworld.hs https://riptutorial.com/zh-TW/home 2 REPL。HaskellGHCghci ghci> putStrLn "Hello World!" Hello, World! ghci> load :l ghci ghci> :load helloworld :reload :r ghci Prelude> :l helloworld.hs [1 of 1] Compiling Main ( helloworld.hs, interpreted ) <some time later after some edits> *Main> :r Ok, modules loaded: Main. main main :: IO () IO ()。 HaskellHindley-Milnermain :: IO () 。main 。。 • Haskell。GHCi“”。 • bug 。。。。 main = putStrLn "Hello, World!" main = do { putStrLn "Hello, World!" ; return () } Haskell; main = do putStrLn "Hello, World!" return () domonadic I / O domonadI / O。 domonadIO returnmonad。 main = putStrLn "Hello, World!" https://riptutorial.com/zh-TW/home 3 putStrLn "Hello, World!"IO () 。“” putStrLn "Hello, World!"main。 putStrLn putStrLn :: String -> IO () -- thus, putStrLn (v :: String) :: IO () putStrLnI / O。mainputStrLn "Hello, World!" 。 Haskell“Hello World” 。 1 fac :: (Integral a) => a -> a fac n = product [1..n] • Integral。IntInteger 。 • (Integral a) =>a • fac :: a -> a facaa • product。 • [1..n]desugarsenumFromTo 1 n 1 ≤ x ≤ n 。 2 fac :: (Integral a) => a -> a fac 0 = 1 fac n = n * fac (n - 1) 。0 。fac。 GHCfac。。 Fibonacci Haskell。 f (0) <- 0 f (1) <- 1 f (n) <- f (n-1) + f (n-2) 。 fibs !! n <- f (n) ┌──────┐ ┌──────┐ ┌──────┐ https://riptutorial.com/zh-TW/home 4 │ f(0) │ │ f(1) │ │ f(2) │ fibs -> 0 : 1 : │ + │ : │ + │ : │ + │ : ..... │ f(1) │ │ f(2) │ │ f(3) │ └──────┘ └──────┘ └──────┘ ┌────────────────────────────────────────┐ │ f(0) : f(1) : f(2) : ..... │ └────────────────────────────────────────┘ -> 0 : 1 : + ┌────────────────────────────────────────┐ │ f(1) : f(2) : f(3) : ..... │ └────────────────────────────────────────┘ fibn n = fibs !! n where fibs = 0 : 1 : map f [2..] f n = fibs !! (n-1) + fibs !! (n-2) GHCi> let fibs = 0 : 1 : zipWith (+) fibs (tail fibs) GHCi> take 10 fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] zipWithzipWith (+) [x1, x2, ...] [y1, y2, ...][x1 + y1, x2 + y2, ...] 。 fibsscanl GHCi> let fibs = 0 : scanl (+) 1 fibs GHCi> take 10 fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] scanlfoldl。 scanl f z0 [x1, x2, ...][z0, z1, z2, ...] where z1 = f z0 x1; z2 = f z1 x2; ... 。fibFibonaccin GHCi> let fib n = fibs !! n -- (!!) being the list subscript operator -- or in point-free style: GHCi> let fib = (fibs !!) GHCi> fib 9 34 REPL HaskellHaskellHaskellREPLread-eval-print-loop。REPLIO。help。Haskell。 GHCI GHCiGlorious / Glasgow Haskell。 GHC。CabalStack。UnixStack curl -sSL https://get.haskellstack.org/ | sh https://riptutorial.com/zh-TW/home 5 GHC。stack。Haskell 。 1.