-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCh5ex.hs
183 lines (121 loc) · 3.78 KB
/
Ch5ex.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
--The Craft of Functional Programing
--Ch. 5 Data Types: Tuples and Lists
module Ch5ex where
import Ch4ex
import Ch3exercises
--5.1
maxOccurs :: Int -> Int -> (Int, Int)
maxOccurs x y
|(x == max x y) && (y == max x y) = (max x y, 2)
| otherwise = (max x y, 1)
maxThreeOccurs :: Int -> Int -> Int -> (Int, Int)
maxThreeOccurs x y z = (maxThree x y z, (howManyOfFourEqual (maxThree x y z) x y z) -1 )
--5.2
middleNumber x y z
|between y x z = x
|between x y z = y
|otherwise = z
orderTriple :: (Int, Int, Int) -> (Int, Int, Int)
orderTriple (x, y, z) = (maxThree x y z, middleNumber x y z, minThree x y z)
--5.3
xInt:: (Float, Float) -> Float
xInt (slope, yint) = negate (slope/yint)
--5.8
doubleAll:: [Int] -> [Int]
doubleAll ex = [2*n| n <- ex ]
--5.9
capitalizeLetters:: String -> String
capitalizeLetters ex = [ toUpper ch | ch <- ex ]
--5.10
divisor :: Int -> Int -> Bool
divisor x i
|mod x i ==0 =True
|otherwise =False
divisors :: Int -> [Int]
divisors n = [x | x <- [n, (n-1) .. 1], divisor n x ]
isPrime :: Int -> Bool
isPrime n
|divisors n == [n, 1] =True
|otherwise =False
--5.11
matches :: Int -> [Int] -> [Int]
matches n list = [ x | x <- list , x==n]
element :: Int -> [Int] -> Bool
element n list
|matches n list == [] = False
|otherwise = True
--5.12
type Person =String
type Book =String
type Database = [(Person, Book)]
exampleBase :: Database
exampleBase = [("Alice", "Tintin"), ("Anna", "Little Women"), ("Alice", "Asterix"), ("Rory", "Tintin")]
books:: Database -> Person -> [Book]
books dBase findPerson = [book | (person, book) <- dBase, person==findPerson]
--5.13
borrowers ::Database -> Book -> [Person]
borrowers dBase findBook = [person | (person, book) <-dBase, book==findBook]
borrowed :: Database -> Book -> Bool
borrowed dBase book
|borrowers dBase book ==[] =False
|otherwise =True
numBorrowed :: Database -> Book -> Int
numBorrowed dBase book = length (borrowers dBase book)
--5.14
returnLoan :: Database -> Person -> Book -> Database
returnLoan dBase pers bk = [pair | pair <-dBase, pair /=(pers, bk)]
--5.16
sing x = [x]
--5.18
shift((x, y), z) = (x, (y, z))
--5.20
romanDigit :: Char -> String
romanDigit n
|n=='1' ="I"
|ord n==50 ="II"
|ord n==51 ="III"
|ord n==52 ="IV"
|ord n==53 ="V"
|ord n==54 ="VI"
|ord n==55 ="VII"
|ord n==56 ="VIII"
|ord n==57 ="IX"
--5.21
onThreeLines :: String -> String -> String -> String
onThreeLines a b c = a++"\n"++b++"\n"++c
--5.22
each :: [String] -> Int -> String
each list n
|n>=0 = each list (n-1) ++ list!!n ++ "\n"
|otherwise =[]
onSeperateLines :: [String] -> String
onSeperateLines list = each list ((length list)-2) ++ list!!((length list)-1)
--["Join", "me", "please"] "Join\nme\nplease\n"
--5.23
duplicate :: String -> Int -> String
duplicate string n
|n>1 =string ++ duplicate string (n-1)
|n==0 =""
--5.24
addSpace :: String -> Int -> String
addSpace string 0 = string
addSpace string n = " "++addSpace string (n-1)
linelength=8
pushRight :: String -> String
pushRight string =addSpace string (linelength-length(string))
--5.25
pushRightOrLeft :: String -> String
pushRightOrLeft string
|linelength>= length(string) = addSpace string (linelength-length(string))
|linelength<length(string) = take linelength string
--5.26
fibStep :: (Int,Int) -> (Int, Int)
fibStep (u, v) =(v, u+v)
fibPair :: Int -> (Int, Int)
fibPair 0 = (0,1)
fibPair n = fibStep(fibPair (n-1))
fastFib :: Int -> Int
fastFib = fst.fibPair
fibTable::Int -> String
fibTable (-1) = "\tn\tfib n"
fibTable n = fibTable(n-1)++"\n\t"++show(n)++"\t"++show(fastFib n)