20 CHAPTER 2. STARTING OUT function we supply. A list produced by a comprehension that draws from two lists of length 4 will have a length of 16, provided we don’t filter them. If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible combinations between numbers in those lists, here’s what we’d do. ghci > [ x*y | x let adjectives = [" lazy "," grouchy "," scheming "] ghci > [ adjective ++ " " ++ noun | adjective [ [ x | x
2.6. TUPLES 21 2.6 Tuples In some ways, tuples are like lists — they are a way to store several values into a single value. However, there are a few fundamental differences. A list of numbers is a list of numbers. That’s its type and it doesn’t matter if it has only one number in it or an infinite amount of numbers. Tuples, however, are used when you know exactly how many values you want to combine and its type depends on how many components it has and the types of the components. They are denoted with parentheses and their components are separated by commas. Another key difference is that they don’t have to be homogenous. Unlike a list, a tuple can contain a combination of several types. Think about how we’d represent a two-dimensional vector in Haskell. One way would be to use a list. That would kind of work. So what if we wanted to put a couple of vectors in a list to represent points of a shape on a twodimensional plane? We could do something like [[1,2],[8,11],[4,5]]. The problem with that method is that we could also do stuff like [[1,2],[8,11,5],[4,5]], which Haskell has no problem with since it’s still a list of lists with numbers but it kind of doesn’t make sense. But a tuple of size two (also called a pair) is its own type, which means that a list can’t have a couple of pairs in it and then a triple (a tuple of size three), so let’s use that instead. Instead of surrounding the vectors with square brackets, we use parentheses: [(1,2),(8,11),(4,5)]. What if we tried to make a shape like [(1,2),(8,11,5),(4,5)]? Well, we’d get this error: Couldn ’t match expected type ‘(t, t1)’ against inferred type ‘(t2 , t3 , t4)’ In the expression : (8 , 11 , 5) In the expression : [(1 , 2) , (8 , 11 , 5) , (4 , 5)] In the definition of ‘it ’: it = [(1 , 2) , (8 , 11 , 5) , (4 , 5)] It’s telling us that we tried to use a pair and a triple in the same list, which is not supposed to happen. You also couldn’t make a list like [(1,2),("One",2)] because the first element of the list is a pair of numbers and the second element is a pair consisting of a string and a number. Tuples can also be used to represent a wide variety of data. For instance, if we wanted to represent someone’s name and age in Haskell, we could use a triple: ("Christopher", "Walken", 55). As seen in this example, tuples can also contain lists. Use tuples when you know in advance how many components some piece of data should have. Tuples are much more rigid because each different size of tuple is its own type, so you can’t write a general function to append an element to a tuple — you’d have to write a function for appending to a pair, one function for appending to a triple, one function for appending to a 4-tuple, etc. While there are singleton lists, there’s no such thing as a singleton tuple. It doesn’t really make much sense when you think about it. A singleton tuple would just be the value it contains and as such would have no benefit to us.
- Page 1 and 2: Learn You a Haskell for Great Good!
- Page 3 and 4: Contents 1 Introduction 5 1.1 About
- Page 5 and 6: Chapter 1 Introduction 1.1 About th
- Page 7 and 8: 1.3. WHAT YOU NEED TO DIVE IN 7 sys
- Page 9 and 10: Chapter 2 Starting Out 2.1 Ready, s
- Page 11 and 12: 2.1. READY, SET, GO! 11 Functions a
- Page 13 and 14: 2.3. AN INTRO TO LISTS 13 Now we’
- Page 15 and 16: 2.3. AN INTRO TO LISTS 15 If you wa
- Page 17 and 18: 2.4. TEXAS RANGES 17 maximum takes
- Page 19: 2.5. I’M A LIST COMPREHENSION 19
- Page 23 and 24: 2.6. TUPLES 23 add a condition that
- Page 25 and 26: Chapter 3 Types and Typeclasses 3.1
- Page 27 and 28: 3.2. TYPE VARIABLES 27 factorial ::
- Page 29 and 30: 3.3. TYPECLASSES 101 29 ghci > 5 /=
- Page 31 and 32: 3.3. TYPECLASSES 101 31 ghci > maxB
- Page 33 and 34: Chapter 4 Syntax in Functions 4.1 P
- Page 35 and 36: 4.1. PATTERN MATCHING 35 Well, that
- Page 37 and 38: 4.2. GUARDS, GUARDS! 37 There’s a
- Page 39 and 40: 4.3. WHERE!? 39 Ugh! Not very reada
- Page 41 and 42: 4.4. LET IT BE 41 cylinder :: ( Rea
- Page 43 and 44: 4.5. CASE EXPRESSIONS 43 case expre
- Page 45 and 46: Chapter 5 Recursion 5.1 Hello recur
- Page 47 and 48: 5.3. A FEW MORE RECURSIVE FUNCTIONS
- Page 49 and 50: 5.4. QUICK, SORT! 49 elem ’ :: (E
- Page 51 and 52: Chapter 6 Higher order functions Ha
- Page 53 and 54: 6.2. SOME HIGHER-ORDERISM IS IN ORD
- Page 55 and 56: 6.2. SOME HIGHER-ORDERISM IS IN ORD
- Page 57 and 58: 6.3. MAPS AND FILTERS 57 " GAYBALLS
- Page 59 and 60: 6.4. LAMBDAS 59 ghci > chain 30 [30
- Page 61 and 62: 6.5. ONLY FOLDS AND HORSES 61 6.5 O
- Page 63 and 64: 6.5. ONLY FOLDS AND HORSES 63 The s
- Page 65 and 66: 6.7. FUNCTION COMPOSITION 65 ($) ::
- Page 67 and 68: 6.7. FUNCTION COMPOSITION 67 fn x =
- Page 69 and 70: Chapter 7 Modules 7.1 Loading modul
- Page 71 and 72:
7.2. DATA.LIST 71 To search for fun
- Page 73 and 74:
7.2. DATA.LIST 73 splitAt takes a n
- Page 75 and 76:
7.2. DATA.LIST 75 False ghci > " ca
- Page 77 and 78:
7.2. DATA.LIST 77 lines is a useful
- Page 79 and 80:
7.3. DATA.CHAR 79 From this, we cle
- Page 81 and 82:
7.3. DATA.CHAR 81 All these predica
- Page 83 and 84:
7.4. DATA.MAP 83 decode :: Int -> S
- Page 85 and 86:
7.4. DATA.MAP 85 It says that it ta
- Page 87 and 88:
7.5. DATA.SET 87 7.5 Data.Set The D
- Page 89 and 90:
7.6. MAKING OUR OWN MODULES 89 and
- Page 91 and 92:
7.6. MAKING OUR OWN MODULES 91 , ar
- Page 93 and 94:
Chapter 8 Making Our Own Types and
- Page 95 and 96:
8.1. ALGEBRAIC DATA TYPES INTRO 95
- Page 97 and 98:
8.2. RECORD SYNTAX 97 O-kay. The fi
- Page 99 and 100:
8.3. TYPE PARAMETERS 99 have an [In
- Page 101 and 102:
8.4. DERIVED INSTANCES 101 had a ty
- Page 103 and 104:
8.4. DERIVED INSTANCES 103 ghci > m
- Page 105 and 106:
8.5. TYPE SYNONYMS 105 ghci > Wedne
- Page 107 and 108:
8.5. TYPE SYNONYMS 107 Fonzie says:
- Page 109 and 110:
8.6. RECURSIVE DATA STRUCTURES 109
- Page 111 and 112:
8.6. RECURSIVE DATA STRUCTURES 111
- Page 113 and 114:
8.7. TYPECLASSES 102 113 | x > a =
- Page 115 and 116:
8.7. TYPECLASSES 102 115 We did it
- Page 117 and 118:
8.8. A YES-NO TYPECLASS 117 instanc
- Page 119 and 120:
8.9. THE FUNCTOR TYPECLASS 119 inst
- Page 121 and 122:
8.9. THE FUNCTOR TYPECLASS 121 In a
- Page 123 and 124:
8.10. KINDS AND SOME TYPE-FOO 123 8
- Page 125 and 126:
8.10. KINDS AND SOME TYPE-FOO 125 a
- Page 127 and 128:
Chapter 9 Input and Output We’ve
- Page 129 and 130:
9.1. HELLO, WORLD! 129 So, when wil
- Page 131 and 132:
9.1. HELLO, WORLD! 131 name. Rememb
- Page 133 and 134:
9.1. HELLO, WORLD! 133 skipped this
- Page 135 and 136:
9.1. HELLO, WORLD! 135 $ runhaskell
- Page 137 and 138:
9.1. HELLO, WORLD! 137 introduced.
- Page 139 and 140:
9.2. FILES AND STREAMS 139 I’m a
- Page 141 and 142:
9.2. FILES AND STREAMS 141 i’m sh
- Page 143 and 144:
9.2. FILES AND STREAMS 143 what we
- Page 145 and 146:
9.2. FILES AND STREAMS 145 contents
- Page 147 and 148:
9.2. FILES AND STREAMS 147 main = d
- Page 149 and 150:
9.2. FILES AND STREAMS 149 thing li
- Page 151 and 152:
9.3. COMMAND LINE ARGUMENTS 151 sec
- Page 153 and 154:
9.3. COMMAND LINE ARGUMENTS 153 han
- Page 155 and 156:
9.4. RANDOMNESS 155 ber, Haskell is
- Page 157 and 158:
9.4. RANDOMNESS 157 with the same g
- Page 159 and 160:
9.4. RANDOMNESS 159 characters and
- Page 161 and 162:
9.5. BYTESTRINGS 161 9.5 Bytestring
- Page 163 and 164:
9.5. BYTESTRINGS 163 chunk even if
- Page 165 and 166:
Chapter 10 Functionally Solving Pro
- Page 167 and 168:
10.1. REVERSE POLISH NOTATION CALCU
- Page 169 and 170:
10.2. HEATHROW TO LONDON 169 natura
- Page 171 and 172:
10.2. HEATHROW TO LONDON 171 Now we
- Page 173 and 174:
10.2. HEATHROW TO LONDON 173 use Se
- Page 175 and 176:
10.2. HEATHROW TO LONDON 175 the ne