-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter6.lhs
More file actions
191 lines (125 loc) · 4.02 KB
/
Copy pathChapter6.lhs
File metadata and controls
191 lines (125 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
Haskell: The Craft of Functional Programming
Simon Thompson
(c) Addison-Wesley, 1999.
Chapter 6
> module Chapter6 where
The Picture example, revisited.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The type of pictures.
> type Picture = [[Char]]
To flip a
picture in a horizontal mirror,
> flipH :: Picture -> Picture
> flipH = reverse
and to place one picture above another it is sufficient to join the two lists of
lines together.
> above :: Picture -> Picture -> Picture
> above = (++)
To flip a picture in a vertical mirror.
> flipV :: Picture -> Picture
> flipV pic
> = [ reverse line | line <- pic ]
To place two pictures side by side.
> sideBySide :: Picture -> Picture -> Picture
> sideBySide picL picR
> = [ lineL ++ lineR | (lineL,lineR) <- zip picL picR ]
To invert the colour of a single character ...
> invertChar :: Char -> Char
> invertChar ch
> = if ch=='.' then '#' else '.'
a line ...
> invertLine :: [Char] -> [Char]
> invertLine line
> = [ invertChar ch | ch <- line ]
and a picture.
> invertColour :: Picture -> Picture
> invertColour pic
> = [ invertLine line | line <- pic ]
Alternative definition of invertColour:
> invertColour' :: Picture -> Picture
> invertColour' pic
> = [ [ invertChar ch | ch <- line ] | line <- pic ]
Extended exercise: positioned pictures
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Positions on a plane.
> type Position = (Int,Int)
An Image is a picture with a position.
> type Image = (Picture,Position)
makeImage :: Picture -> Position -> Image
changePosition :: Image -> Position -> Image
moveImage :: Image -> Int -> Int -> Image
printImage :: Image -> IO ()
Local definitions
^^^^^^^^^^^^^^^^^
The sum of the squares of two numbers.
> sumSquares :: Int -> Int -> Int
> sumSquares n m
> = sqN + sqM
> where
> sqN = n*n
> sqM = m*m
Add corresponding elements in two lists; lists truncated to the length of the
shorter one.
> addPairwise :: [Int] -> [Int] -> [Int]
> addPairwise intList1 intList2
> = [ m + n | (m,n) <- zip intList1 intList2 ]
A variant of addPairwise which doesn't truncate; see book for details of how
it works.
> addPairwise' :: [Int] -> [Int] -> [Int]
> addPairwise' intList1 intList2
> = front ++ rear
> where
> minLength = min (length intList1) (length intList2)
> front = addPairwise (take minLength intList1)
> (take minLength intList2)
> rear = drop minLength intList1 ++ drop minLength intList2
and a variant of this ...
> addPairwise'' :: [Int] -> [Int] -> [Int]
> addPairwise'' intList1 intList2
> = front ++ rear
> where
> minLength = min (length intList1) (length intList2)
> front = addPairwise front1 front2
> rear = rear1 ++ rear2
> (front1,rear1) = splitAt minLength intList1
> (front2,rear2) = splitAt minLength intList2
Let expressions
^^^^^^^^^^^^^^^
Two examples which use `let'.
> letEx1 :: Int
> letEx1 = let x = 3+2 in x^2 + 2*x - 4
> letEx2 :: Int
> letEx2 = let x = 3+2 ; y = 5-1 in x^2 + 2*x - y
Scopes
^^^^^^
Is a value odd? even?
> isOdd, isEven :: Int -> Bool
> isOdd n
> | n<=0 = False
> | otherwise = isEven (n-1)
> isEven n
> | n<0 = False
> | n==0 = True
> | otherwise = isOdd (n-1)
Extended exercise: supermarket billing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Types of names, prices (pence) and bar-codes.
> type Name = String
> type Price = Int
> type BarCode = Int
The database linking names prices and bar codes.
> type Database = [ (BarCode,Name,Price) ]
The example database we use is
> codeIndex :: Database
> codeIndex = [ (4719, "Fish Fingers" , 121),
> (5643, "Nappies" , 1010),
> (3814, "Orange Jelly", 56),
> (1111, "Hula Hoops", 21),
> (1112, "Hula Hoops (Giant)", 133),
> (1234, "Dry Sherry, 1lt", 540)]
The lists of bar codes, and of Name,Price pairs.
> type TillType = [BarCode]
> type BillType = [(Name,Price)]
The length of a line in the bill.
> lineLength :: Int
> lineLength = 30