-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedinburgh1.hs
More file actions
75 lines (61 loc) · 2.18 KB
/
Copy pathedinburgh1.hs
File metadata and controls
75 lines (61 loc) · 2.18 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
---Problem 1
halveEvens :: [Int] -> [Int]
halveEvens xs = [div x 2| x <- xs, even x]
---Problem 2
halveEvensRec :: [Int] -> [Int]
halveEvensRec [] = []
halveEvensRec (x:xs) | even x = [div x 2]++ halveEvensRec(xs)
| otherwise = halveEvensRec(xs)
---Problem 3
inRange :: Int -> Int -> [Int] -> [Int]
inRange lo hi xs = [x|x <- xs, x>=lo && x <= hi ]
---Problem 4
inRangeRec :: Int -> Int -> [Int] -> [Int]
inRangeRec lo hi [] = []
inRangeRec lo hi (x:xs) | (x>=lo) && (x<=hi) = [x] ++ inRangeRec lo hi (xs)
| otherwise = inRangeRec lo hi (xs)
---Problem 5
countPositives :: [Int] -> Int
countPositives xs = length[x | x <- xs , x > 0]
---Problem 6
countPositivesRec :: [Int] -> Int
countPositivesRec [] = 0
countPositivesRec (x:xs) | (x>0) = 1+ ( countPositivesRec (xs) )
| otherwise = countPositivesRec (xs)
--- Problem 7
-- Helper function
discount :: Int -> Int
discount price = round(fromIntegral(price) * 0.9)
-- List-comprehension version of pennypincher
pennypincher :: [Int] -> Int
pennypincher prices = sum [discount x | x <- prices, discount x <= 19900]
--- Problem 8
-- Helper function
discount :: Int -> Int
discount price = round(fromIntegral(price) * 0.9)
-- Recursive version
pennypincherRec :: [Int] -> Int
pennypincherRec [] = 0
pennypincherRec (price:prices) | (discount price) <= 19900 = discount price + pennypincherRec prices
| otherwise = pennypincherRec prices
--- Problem 9
multDigits :: String -> Int
multDigits str = product [digitToInt c | c <- str, isDigit c]
--- Problem 10
multDigitsRec :: String -> Int
multDigitsRec [] = 1
multDigitsRec (x:xs) | isDigit x = (digitToInt x) * multDigitsRec xs
| otherwise = multDigitsRec xs
--- Problem 11
capitalise :: String -> String
capitalise [] = []
capitalise str = (toUpper (head str)) : [toLower y | y <- tail str]
--- Problem 12
-- Recursive version
capitaliseRec :: String -> String
capitaliseRec [] = []
capitaliseRec (x:xs) = toUpper x : lowerRec xs
-- Helper function
lowerRec :: String -> String
lowerRec [] = []
lowerRec (x:xs) = toLower x : lowerRec xs