-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter12.hs
189 lines (121 loc) · 3.88 KB
/
Chapter12.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
-----------------------------------------------------------------------
--
-- Haskell: The Craft of Functional Programming, 3e
-- Simon Thompson
-- (c) Addison-Wesley, 1996-2011.
--
-- Chapter 12
--
-----------------------------------------------------------------------
-- For Rock-Paper-Scissors examples see RPS.hs
module Chapter12 where
import Pictures hiding (flipH,rotate,flipV,beside,invertColour,
superimpose,printPicture)
-- Revisiting the Pictures example, yet again.
flipV :: Picture -> Picture
flipV = map reverse
beside :: Picture -> Picture -> Picture
beside = zipWith (++)
-- Revisiting the Picture example
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- Some of the functions are already (re)defined in this script.
-- Among the other functions mentioned were
invertColour :: Picture -> Picture
invertColour = map (map invert)
superimpose :: Picture -> Picture -> Picture
superimpose = zipWith (zipWith combineChar)
-- The definition of combineChar is left as an exercise: it's a dummy definition
-- here.
combineChar :: Char -> Char -> Char
combineChar = combineChar
-- Printing a picture: uses putStr after a newline has been added at the end of
-- every line and the lines are joined into a single string.
printPicture :: Picture -> IO ()
printPicture = putStr . concat . map (++"\n")
-- Regular expressions
type RegExp = String -> Bool
char :: Char -> RegExp
epsilon = (=="")
char ch = (==[ch])
(|||) :: RegExp -> RegExp -> RegExp
e1 ||| e2 =
\x -> e1 x || e2 x
(<*>) :: RegExp -> RegExp -> RegExp
e1 <*> e2 =
\x -> or [ e1 y && e2 z | (y,z) <- splits x ]
(<**>) :: RegExp -> RegExp -> RegExp
e1 <**> e2 =
\x -> or [ e1 y && e2 z | (y,z) <- fsplits x ]
splits xs = [splitAt n xs | n<-[0..len]]
where
len = length xs
star :: RegExp -> RegExp
star p = epsilon ||| (p <**> star p)
-- epsilon ||| (p <*> star p)
-- is OK as long as p can't have epsilon match
fsplits xs = tail (splits xs)
--
-- Case studies: functions as data
--
-- Natural numbers as functions.
type Natural a = (a -> a) -> (a -> a)
zero, one, two :: Natural a
zero f = id
one f = f
two f = f.f
int :: Natural Int -> Int
int n = n (+1) 0
-- sends representation of n to rep. of n+1
succ :: Natural a -> Natural a
succ = error "succ"
-- sends reps. of n and m to rep. of n+m
plus :: Natural a -> Natural a -> Natural a
plus = error "plus"
-- sends reps. of n and m to rep. of n*m
times :: Natural a -> Natural a -> Natural a
times = error "times"
-- Creating an index
-- ^^^^^^^^^^^^^^^^^
-- See Index.hs
-- Development in practice
-- ^^^^^^^^^^^^^^^^^^^^^^^
-- Defining the .. notation (not executable code).
--
-- [m .. n]
-- | m>n = []
-- | otherwise = m : [m+1 .. n]
-- [1 .. n]
-- | 1>n = []
-- | otherwise = [1 .. n-1] ++ [n]
-- A simple palindrome check.
simplePalCheck :: String -> Bool
simplePalCheck st = (reverse st == st)
-- The full check
palCheck = simplePalCheck . clean
-- where the clean function combines mapping (capitals to smalls) and
-- filtering (removing punctuation)
clean :: String -> String
clean = map toSmall . filter notPunct
toSmall = toSmall -- dummy definition
notPunct = notPunct -- dummy definition
-- Auxiliary functions
-- When is one string a subsequence of another?
subseq :: String -> String -> Bool
subseq [] _ = True
subseq (_:_) [] = False
subseq (x:xs) (y:ys)
= subseq (x:xs) ys || frontseq (x:xs) (y:ys)
-- When is one strong a subsequece of another, starting at the front?
frontseq :: String -> String -> Bool
frontseq [] _ = True
frontseq (_:_) [] = False
frontseq (x:xs) (y:ys)
= (x==y) && frontseq xs ys
-- Understanding programs
-- ^^^^^^^^^^^^^^^^^^^^^^
mapWhile :: (a -> b) -> (a -> Bool) -> [a] -> [b]
mapWhile f p [] = []
mapWhile f p (x:xs)
| p x = f x : mapWhile f p xs
| otherwise = []
example1 = mapWhile (2+) (>7) [8,12,7,13,16]