-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrivialFuncs.hs
93 lines (74 loc) · 2.37 KB
/
TrivialFuncs.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
module TrivialFuncs where
numDivs :: Int -> Int -> Int
-- i must be 2
numDivs 1 _ = 1
numDivs n i =
if i * i < n
then if n `mod` i == 0
then 2 + numDivs n (i+1)
else numDivs n (i+1)
else if i * i == n
then 3
else 2
toInt :: Float -> Float
toInt x = x - restDiv x 1
restDiv :: Float -> Float -> Float
restDiv x n
| x >= n = restDiv (x - n) n
| otherwise = x
squash' :: [a] -> [b] -> [(a, b)]
squash' [] _ = []
squash' _ [] = []
squash' (x:xs) (y:ys) = (x, y):squash' xs ys
elementI :: [a] -> Float -> a
elementI x n = (\[(z, _)] -> z) . filter (\(_, y) -> y == round n) $ squash' x [0..length x - 1]
lastElement :: [a] -> a
lastElement [x] = x
lastElement (x:xs) = lastElement xs
sumList :: [Float] -> Float
sumList [] = 0
sumList (x:xs) = x + sumList xs
sizeList :: [a] -> Float
sizeList x = sumList [1 | n <- x]
sumListSquare :: [Float] -> Float
sumListSquare [] = 0
sumListSquare (x:xs) = x*x + sumListSquare xs
repeatTimes :: Float -> [Float] -> Int
repeatTimes x [] = 1
repeatTimes x (y:ys)
| y == x = 1 + repeatTimes x ys
| otherwise = repeatTimes x ys
repeatAll :: [Float] -> [Int]
repeatAll [] = []
repeatAll (x:xs) = repeatTimes x xs:repeatAll xs
riemmansum :: Double -> Double -> Double -> Double -> String -> Double
riemmansum a b n sum y =
if (a < b - prec) then riemmansum (a + (b - a) / n) b n (sum + calculate (changeArg y a) "" * ((b - a) / n)) y else sum
where prec = 0.00001
integral :: Double -> Double -> String -> Double
integral a b y = riemmansum a b n 0 y
where n = 10000
dummyfunction :: Double -> Double
dummyfunction x = x
changeArg :: String -> Double -> String
changeArg [] _ = ""
changeArg (a:b) n = if a == 'x' then (show n) ++ changeArg b n else a:changeArg b n
get_op :: String -> String -> Double
get_op (a:b:c) n =
if b == ' ' then
if a == '+' then read n + calculate c [] else
if a == '-' then read n - calculate c [] else
if a == '*' then read n * calculate c [] else
read n / calculate c []
else read n
calculate :: String -> String -> Double
calculate [] n = read n
calculate (a:b) n =
if a == ' ' then get_op b n else calculate b (n ++ [a])
--input
firstArgument :: String -> String
firstArgument (' ':xs) = []
firstArgument (x:xs) = x:firstArgument xs
secondArgument :: String -> [Float]
secondArgument (' ':xs) = read xs :: [Float]
secondArgument (x:xs) = secondArgument xs