-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.hs
169 lines (125 loc) · 4.35 KB
/
functions.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
-- This is head comment (comment should be preceded by two dashes)
-- Assignment 2, CSCE 314
-- Student Name: Jack Shirley
-- UIN: 624003478
-- Help received: Programming in Haskell Textbook, Dr. Dutta's slides, and reference to the standard prelude online
module Main where
import Test.HUnit
import System.Exit
import Data.List
-- Problem 2
fibonacci :: Int -> Int
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = (fibonacci (n - 1)) + (fibonacci (n - 2))
-- Problem 3
myProduct :: [Integer] -> Integer
myProduct [] = 1
myProduct (x:xs) = x * (myProduct xs)
-- Problem 4
flatten :: [[a]] -> [a]
flatten [] = []
flatten (xs:ys) = xs ++ flatten ys
--flatten xss = [x | xs <- xss, x <- xs]
-- Problem 5
myLength :: [a] -> Int
myLength [] = 0
myLength [x] = 1
myLength (_:xs) = 1 + myLength xs
-- Problem 6
quicksort :: Ord t => [t] -> [t]
quicksort [] = []
quicksort (x:xs) = quicksort smaller ++ [x] ++ quicksort larger
where
smaller = [a | a <- xs, a <= x]
larger = [b | b <- xs, b > x]
-- Problem 7
isElement :: Eq a => a -> [a] -> Bool
isElement y [] = False
isElement y (x:xs) = if y == x then True else isElement y xs
-- Problem 8
isort :: [Int] -> [Int]
isort [] = []
isort (x:xs) = insert x (isort xs)
-- Problem 9
riffle :: [a] -> [a] -> [a]
riffle xs [] = xs
riffle [] ys = ys
riffle (x:xs) (y:ys) = x : y : (riffle xs ys)
-- Problem 10
fh :: [a] -> [a]
fh xs = if (myLength xs `mod` 2) == 0 then take (myLength xs `div` 2) xs else error "Odd list"
sh :: [a] -> [a]
sh xs = if (myLength xs `mod` 2) == 0 then drop (myLength xs `div` 2) xs else error "Odd list"
shuffle :: Int -> [a] -> [a]
shuffle 0 xs = xs
shuffle n xs = shuffle (n-1) (riffle (fh xs) (sh xs))
-- Problem 11
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
perfects :: Int -> [Int]
perfects y = [x | z <- [1..y], x <- (factors z), (sum ((factors x)) - z) == z]
-- Problem 12
replicate :: Int -> a -> [a]
replicate 0 _ = []
replicate n z = [z | y <- [1..n], y <= n]
myTestList =
TestList [
"fibonacci" ~: fibonacci 4 ~=? 3
, "myProduct" ~: myProduct [1..10] ~=? 3628800
, "flatten 1" ~: flatten [[]::[Int]] ~=? []
, "flatten 2" ~: flatten [[]::[Int], [], []] ~=? []
, "flatten 3" ~: flatten [[1], [2, 3, 4], [], [5, 6]] ~=? [1, 2, 3, 4, 5, 6]
, "myLength" ~: myLength [1, 2, 3] ~=? 3
, "quicksort 1" ~: quicksort [3, 2, 5, 1, 6] ~=? [1,2,3,5,6]
, "quicksort 2" ~: quicksort "howdy" ~=? "dhowy"
, "isElement 1" ~: (isElement 'c' "abcd") ~=? True
, "isElement 2" ~: (isElement 'e' "abcd") ~=? False
]
main = do c <- runTestTT myTestList
putStrLn $ show c
let errs = errors c
fails = failures c
exitWith (codeGet errs fails)
codeGet errs fails
| fails > 0 = ExitFailure 2
| errs > 0 = ExitFailure 1
| otherwise = ExitSuccess
fh :: [a] -> [a]
fh xs = if (myLength xs `mod` 2) == 0 then take (myLength xs `div` 2) xs else error "Odd list"
sh :: [a] -> [a]
sh xs = if (myLength xs `mod` 2) == 0 then drop (myLength xs `div` 2) xs else error "Odd list"
shuffle :: Int -> [a] -> [a]
shuffle 0 xs = xs
shuffle n xs = shuffle (n-1) (riffle (fh xs) (sh xs))
-- Problem 11
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
perfects :: Int -> [Int]
perfects y = [x | z <- [1..y], x <- (factors z), (sum ((factors x)) - z) == z]
-- Problem 12
replicate :: Int -> a -> [a]
replicate 0 _ = []
replicate n z = [z | y <- [1..n], y <= n]
myTestList =
TestList [
"fibonacci" ~: fibonacci 4 ~=? 3
, "myProduct" ~: myProduct [1..10] ~=? 3628800
, "flatten 1" ~: flatten [[]::[Int]] ~=? []
, "flatten 2" ~: flatten [[]::[Int], [], []] ~=? []
, "flatten 3" ~: flatten [[1], [2, 3, 4], [], [5, 6]] ~=? [1, 2, 3, 4, 5, 6]
, "myLength" ~: myLength [1, 2, 3] ~=? 3
, "quicksort 1" ~: quicksort [3, 2, 5, 1, 6] ~=? [1,2,3,5,6]
, "quicksort 2" ~: quicksort "howdy" ~=? "dhowy"
, "isElement 1" ~: (isElement 'c' "abcd") ~=? True
, "isElement 2" ~: (isElement 'e' "abcd") ~=? False
]
main = do c <- runTestTT myTestList
putStrLn $ show c
let errs = errors c
fails = failures c
exitWith (codeGet errs fails)
codeGet errs fails
| fails > 0 = ExitFailure 2
| errs > 0 = ExitFailure 1
| otherwise = ExitSuccess