-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChapter6.hs
225 lines (148 loc) · 4.98 KB
/
Chapter6.hs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
--------------------------------------------------------------------------
--
-- Haskell: The Craft of Functional Programming, 3e
-- Simon Thompson
-- (c) Addison-Wesley, 1996-2011.
--
-- Chapter 6
--
--------------------------------------------------------------------------
module Chapter6 where
import Prelude hiding (id)
import Test.QuickCheck
-- Polymorphism
-- ^^^^^^^^^^^^
-- Defining the identity function
id :: a -> a
id x = x
-- Extracting the first element of a pair.
fst :: (a,b) -> a
fst (x,y) = x
-- A "mystery" function
mystery :: (Bool,a) -> Char
mystery (x,y) = if x then 'c' else 'd'
-- 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.
beside :: Picture -> Picture -> Picture
beside 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 ]
-- Properties for Pictures
-- ^^^^^^^^^^^^^^^^^^^^^^^
prop_AboveFlipV, prop_AboveFlipH :: Picture -> Picture -> Bool
prop_AboveFlipV pic1 pic2 =
flipV (pic1 `above` pic2) == (flipV pic1) `above` (flipV pic2)
prop_AboveFlipH pic1 pic2 =
flipH (pic1 `above` pic2) == (flipH pic1) `above` (flipH pic2)
propAboveBeside :: Picture -> Picture -> Picture -> Picture -> Bool
propAboveBeside nw ne sw se =
(nw `beside` ne) `above` (sw `beside` se)
==
(nw `above` sw) `beside` (ne `above` se)
propAboveBeside3Correct :: Picture -> Picture -> Property
propAboveBeside3Correct w e =
(rectangular w && rectangular e && height w == height e)
==>
(w `beside` e) `above` (w `beside` e)
==
(w `above` w) `beside` (e `above` e)
rectangular :: Picture -> Bool
rectangular = error "for you to define"
height :: Picture -> Int
height = error "for you to define"
-- 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 :: Integer -> Integer -> Integer
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 :: [Integer] -> [Integer] -> [Integer]
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' :: [Integer] -> [Integer] -> [Integer]
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'' :: [Integer] -> [Integer] -> [Integer]
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
-- 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