-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter11.lhs
More file actions
63 lines (41 loc) · 1.25 KB
/
Copy pathChapter11.lhs
File metadata and controls
63 lines (41 loc) · 1.25 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
Haskell: The Craft of Functional Programming
Simon Thompson
(c) Addison-Wesley, 1999.
Chapter 11
> module Chapter11 where
Program development
^^^^^^^^^^^^^^^^^^^
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