Learn You a Haskell for Great Good! © 2011 by Miran Lipovača
Total Page:16
File Type:pdf, Size:1020Kb
INDEX Symbols & Numbers !! (double exlamation point) in Data.List module, 182 && (double ampersand) using with lists, 9 as Boolean operator conjunction, 2 > (greater-than) operator, using with using with folds and lists, 78–79 lists, 9–10 '(apostrophe) >> function, replacing, 279 using with functions, 7 >>= (bind) function using with types, 149–150 in A Knight’s Quest, 292 * (asterisk) nested use of, 280 as multiplication function, 3 using with functions as monads, 311 using with kinds, 150 using with monads, 269–270, 272, ** (exponentiation), using with RPN func- 274–280, 283–284, 286 tions, 207–208 using with Reader monad, 312 \ (backslash), declaring lambdas with, 71 using with State monad, 316–317 ` (backticks) using with functions, 4–5 using with Writer type, 302 : (colon) -> (arrow) as cons operator in type signature, 60–61 bytestring version of, 200 using with functions, 25 using with applicatives, 238–239 using with lambdas, 71 using with lists, 8–9 -> r as functor and monad, 311 using with infix constructors, 134 < (less-than) operator, using with lists, 9–10 :: (double colon) <*> function using in record syntax, 116 calling with applicative values, 236 using with type annotations, 30, 118 left-associative, 233 using with types, 24 specializing for IO, 234 :k command, identifying kinds with, using with applicative style, 232 150–151 using with liftM function, 325 $ (function application operator), using with zip lists, 237 80–81, 83 <= operator, using with lists, 9–10 / (division), using with RPN functions, <$>, using with applicative style, 231–232 207–208 <-, using with I/O actions and /= (not-equal-to) operator, 3, 28 functors, 219 = (equal) sign - (minus) operator, using with sections, 62 using with data keyword, 109 () (parentheses) using with data types, 122 minimizing use of, 81, 83 using with functions, 5 placement with functions, 7 == (double equal sign), 3 using with operations, 2, 5 using with Eq type class, 28 using with sections, 62 using with type instances, 139–140 (,,) function, using with zip lists, 238 Learn You a Haskell for Great Good! © 2011 by Miran Lipovača . (period), using with functions, 89 liftA2 function, 238–239 .. (dots), using with value constructors, lists as, 232–234, 243–244, 285–287 113–114 Maybe types as, 269–270 + (plus) operator, 3, 5 sequenceA function, 239–242 ++ (concatenation) operator upgrading, 267–269 excluding from pattern matching, 40 zip lists, 237 using with lists, 8 applicative laws, 238 ; (semicolon), using with let expressions, 46 applicative operators, vs. monads, 278 [] (square brackets), using with lists, 7, 24 applicative style, using on lists, 233–234 [Char] and String types, 30, 127–128 Applicative type class, 228–229, 323 _ (underscore) Maybe implementation, 229–230 in pattern matching, 38 style of pure, 230–232 using with lists, 18 applyLog function | (vertical pipe) using with monoids, 300 using with data keyword, 109 using with Writer monad, 299–300 using with data types, 122 arithmetic expressions, 2 using with guards, 41 arrow (->) || as Boolean operator disjunction, 2, 256 in type signature, 60–61 0 flag, using in Heathrow to London using with functions, 25 example, 216 using with lambdas, 71 3D vector type, implementing, 121–122 askForNumber function, 197 as-pattern, 40 A association lists, 98–100. See also lists associativity accumulators defined, 251 using with folds, 73 using with monads, 294–296 using with right folds, 75 asterisk (*) using with scanl and scanr, 79–80 as multiplication function, 3 addDrink function, 301–302 using with kinds, 150 algebraic data structures, 137 algebraic data types, 126–127, 133. See also B data types algebraic expressions, writing, 203–208 baby.hs file All type, using with monoids, 257 appending code to, 6 ampersands (&&) saving, 5 as Boolean operator conjunction, 2 backslash (\), declaring lambdas with, 71 using with folds and lists, 78–79 backticks (`) using with functions, 4–5 and function Banana on a Wire example, 278–280 using with applicative functors, 241 base case, reaching, 51 using with lists, 78 binary functions any function, 92 using on values, 251 Any newtype constructor, using with using with folds, 73 monoids, 256–257 binary search tree, implementing, 135–137 apostrophe (') bind (>>=) function using with functions, 7 in A Knight’s Quest, 292 using with types, 149–150 nested use of, 280 appendFile function using with functions as monads, 311 in to-do list example, 180 using with monads, 269–270, 272, using in I/O, 180 274–280, 283–284, 286 applicative functors, 227–228, 237–238, using with Reader monad, 312 323. See also functors using with State monad, 316–317 Applicative type class, 228–229, 323 using with Writer type, 302 functions as, 235–236 binding to variables, 39 364 INDEX Learn You a Haskell for Great Good! © 2011 by Miran Lipovača birds code blocks, excluding, 48–49 ignoring in Pierre example, 278–280 coin-toss function, 193–195 representing in Pierre example, 275–278 Collatz sequence, 69–70 BMI (body mass index) colon (:) calculation of, 41–42 as cons operator listing of, 45 bytestring version of, 200 repeating calculations of, 43 using with applicatives, 238–239 Boolean algebra, 2 using with lists, 8–9 Boolean expressions, using with guards, 41 using with infix constructors, 134 Boolean values command-line arguments, 184–185 generating randomly, 191 compare function for tossing coin, 193 using Ordering type with, 29 Bool type, 26, 143–144, 256–257 using with guards, 42 Bounded type class, 31–32, 126–127 using with monoids, 259 bracket function, using in I/O, 178–179 computations bracketOnError function, 183–184 deferred, 199 breadcrumbs performing, 52 in filesystem, 355 concatenation (++) operator representing in trees, 346–348 excluding from pattern matching, 40 using with lists and zippers, 352–353 using with lists, 8 bytestrings, 198–202. See also lists concrete types, 150–151. See also data types changing types of, 300 conditions, adding to list comprehen- copying files with, 201–202 sions, 16 module functions, 201 conjunction (&&) Boolean operator, 2 as monoids, 300 cons (:) operator, using with lists, 8–9 strict and lazy, 199–201 Cons constructor, 133 context of failure, adding to values, 321. C Control.Exception bracketOnError, 183–184 Caesar cipher, 92–94 copyFile function, 201 calculations, performing once, 42–45 copying files with bytestrings, 201–202 capital letters, restriction of, 7 Cube.hs file in Geometry module, 107 capslocker.hs program Cuboid.hs file in Geometry module, 106 exiting, 171 curried functions, 59–62, 222 getContents I/O action, 171 max, 60 saving and compiling, 170 printing functions, 63 Car data type, 119–120 sections, 62–63 case expressions, 48–49 cycle function, using with lists, 14 vs. if else statements, 48 vs. let expressions, 48 D syntax, 48 cat program, 180–181 Data.ByteString.Lazy module, 199 characters Data.Char module, 93, 96 converting into numbers, 96 data keyword, 109–110 shifting, 93 vs. newtype, 244–245, 248–249 [Char] and String types, 30, 127–128 using, 250 CharList value constructor, 245–246, 250 Data.List module, 88–89. See also lists Char type, 26 !! function, 182 chessboard example, 290–292 any function, 92 circles, representing, 110–112 delete function, 182 class constraints, 140, 142 group function, 90 class declarations, 140 tails function, 91–92 words function, 90 Learn You a Haskell for Great Good! INDEX 365 © 2011 by Miran Lipovača Data.Map module, 114. dots (..), using with value constructors, fromListWith function, 103 113–114 lookup function, 100 double colon (::) Map k v parameterized type, 120 using in record syntax, 116 Data.Monoid module using with type annotations, 30, 118 Product type, 255–256 using with types, 24 Sum type, 255–256 double equal sign (==), 3 data structures. See also zippers using with Eq type class, 28 reducing to values, 73 using with type instances, 139–140 using zippers with, 352 Double type, 26 data types. See also algebraic data types; drop function, using with lists, 12 concrete types; recursive data struc- tures; type constructors; type param- E eters; types 3D vector, 121–122 Either, kind of, 151 applying to type constructors, 150–152 Either a b type, 130–132, 149–150 defining, 109–110, 122 Either e a type, 321–322 for describing people, 114–117, 123–124 elem function identifying, 150–151 using recursively, 55–56 making, 250 using with lists, 12 record syntax, 116–117 end-of-file character, issuing, 170 wrapping with newtype keywords, Enum type class, 31, 126–127 244–245 equal (=) sign Day type, 127 using with data keyword, 109 deferred computation, 199 using with data types, 122 definitions, functions as, 7 using with functions, 5 deletetodo.hs program, saving and compil- equality (== and /=) operators, 3 ing, 182 equality testing, 28 derived instances, 122–127. See also type Eq type class, 28, 122–124, 138–139, classes 141, 250 equating people, 123–124 erroneous computation, representing, 247 Read type class, 124–125 error function Show type class, 124–125 calling, 178 deriving keyword, using with newtype, 245 using in pattern matching, 39 dictionaries, 98 Error instance, 322 difference lists, using, 307–309 error messages, 3 digitToInt function, 96 Euclid’s algorithm, 304–305 disjunction (||) Boolean operator, 2 exceptions, raising, 178, 247–248 div function, 4–5 exponentiation (**), using with RPN func- division (/), using with RPN functions, tions, 207–208 207–208 exporting do expressions. See also monads functions, 104 actions of, 219 shapes in modules, 113–114 failure of pattern matching in, 284 expressions let lines in, 282 determining types of, 24 monadic expressions in, 282 equivalent examples of, 71–72 monadic values in, 282 lambdas as, 71 results of, 318 using operations in, 2 writing, 283 do notation, 280–285, 290 and <-, 156 F and list comprehensions, 288 factorial function, 25, 36 pattern matching and failure, 284–285 failure, adding context of, 321 using with Writer monad, 303–304 False Boolean value, 2–3 366 INDEX Learn You a Haskell for Great Good! © 2011 by Miran Lipovača Fibonacci sequence, specifying recur- function application operator ($), sively, 51–52 80–81, 83 file contents vs.