Advertisement
thouxanbanuno

lab2

May 26th, 2021
1,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Lab1 where
  2.  
  3. data Point = Point Float Float deriving (Show, Eq)
  4. data Pair x y = Pair x y deriving (Show)
  5. firstPair :: Pair x y -> x
  6. firstPair (Pair x y) = x
  7.  
  8.  
  9. data Shape = Circle Float Float Float | Rectangle Float Float Float Float
  10. areaShape :: Shape -> Float
  11. areaShape (Circle _ _ r) = pi * r^2
  12. areaShape (Rectangle x1 y1 x2 y2) = abs(x2 - x1) * abs(y2 - y1)
  13.  
  14. data Color = Red | Green | Blue | RGB Int Int Int
  15. redComponent :: Color -> Int
  16. redComponent Red = 255
  17. redComponent _ = 0
  18.  
  19. solve :: Float -> Float -> Maybe Float
  20. solve 0 b = Nothing
  21. solve a b = Just (-b/a)
  22.  
  23. r_sorted:: Int -> [Int]
  24. r_sorted 0  = []
  25. r_sorted n = n:r_sorted (n - 1)
  26.  
  27. sorted :: Int -> [Int]
  28. sorted n = reverse(r_sorted n)
  29.  
  30.  
  31.  
  32. r_odds :: Int -> [Int]
  33. r_odds 0 = []
  34. r_odds n = if odd n then n:r_odds(n-1) else r_odds(n-1)
  35.  
  36. odds :: Int -> [Int]
  37. odds n = reverse(r_odds n)
  38.  
  39. r_evens :: Int -> [Int]
  40. r_evens 0 = []
  41. r_evens n = if even n then n:r_evens(n-1) else r_evens(n-1)
  42.  
  43. evens :: Int -> [Int]
  44. evens n = reverse(r_evens n)
  45.  
  46. r_cubes :: Int  -> [Int]
  47. r_cubes 0  = []
  48. r_cubes  n = (n)*(n)*(n):r_cubes (n - 1)
  49.  
  50. cubes :: Int -> [Int]
  51. cubes n = reverse(r_cubes n)
  52.  
  53. fact :: (Integral a) => a -> a
  54. fact 0 = 1
  55. fac n = n * fact(n - 1)
  56.  
  57. r_facts :: Int -> [Int]
  58. r_facts 0 = [1]
  59. r_facts n = (fact n) :r_facts(n-1)
  60.  
  61. facts :: Int -> [Int]
  62. facts n = reverse(r_facts n)
  63.  
  64. r_tens :: Int -> [Int]
  65. r_tens (-1) = []
  66. r_tens n = 10^n :r_tens (n - 1)
  67.  
  68. tens :: Int -> [Int]
  69. tens n = reverse(r_tens n)
  70.  
  71. treug :: Int -> Int
  72. treug 1 = 1
  73. treug n = n + treug (n-1)
  74. treugs :: Int -> [Int]
  75. treugs 0 = []
  76. treugs x = treugs (x-1) ++ (treug x : [])
  77.  
  78. piram :: Int -> Int
  79. piram 1 = 1
  80. piram n = treug n + piram (n-1)
  81. pirams :: Int -> [Int]
  82. pirams n = [piram x | x <- [1 .. n]]
  83.  
  84.  
  85. avg' :: [Double] -> Double -> Double -> Double
  86. avg' [] n s = s/n
  87. avg' (x:xs) n s = avg' xs (n+1) (s+x)
  88. avg :: [Double] -> Double
  89. avg x = avg' x 0 0
  90.  
  91. removeItem :: Int -> [Int] -> [Int]
  92. removeItem n (x:xs) = if n == 1 then xs else x : removeItem (n-1) xs
  93.  
  94. sums :: [Int] -> [Int] -> [Int]
  95. sums [] [] = []
  96. sums (x:xs) [] = x:xs
  97. sums [] (y:ys) = y:ys
  98. sums (x:xs) (y:ys) = (x + y) : sums xs ys
  99.  
  100. swapEvenOdd :: [Int] -> [Int]
  101. swapEvenOdd [] = []
  102. swapEvenOdd [x] = [x]
  103. swapEvenOdd (x:y:xs) = y : x : swapEvenOdd xs
  104.  
  105. twopow :: Int -> Int
  106. twopow n | (n == 1) = 2
  107.         | (even n) = w * w
  108.         | otherwise = (w * w) + (w * w)
  109.         where w = twopow (n `div` 2)
  110.  
  111. removeOdd :: [Int] -> [Int]
  112. removeOdd [] = []
  113. removeOdd (x:xs) = if odd x then removeOdd xs else x : removeOdd xs
  114.  
  115. removeEmpty :: [String] -> [String]
  116. removeEmpty [] = []
  117. removeEmpty (x:xs) = if x == "" then removeEmpty xs else x : removeEmpty xs
  118.  
  119.  
  120.  
  121. countTrue :: [Bool] -> Integer
  122. countTrue [] = 0
  123. countTrue (x:xs) = if x  then 1 + countTrue xs else countTrue xs
  124.  
  125. makePositive :: [Int] -> [Int]
  126. makePositive [] = []
  127. makePositive (x:xs) = if x < 0 then (-x) : makePositive xs else x : makePositive xs
  128.  
  129. delete :: Char -> String -> String
  130. delete _ "" = ""
  131. delete x (y:ys) = if x == y then delete x ys else y : delete x ys
  132.  
  133. substitute :: Char -> Char -> String -> String
  134. substitute _ _ "" = ""
  135. substitute x y (z:zs) = if x == z then y : substitute x y zs else z : substitute x y zs
  136.  
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement