Advertisement
O_Egor

LAB2 HASKELLLLLL

May 23rd, 2021
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. module Lab2 where
  2.  
  3. fact :: Integer -> Integer
  4. fact x
  5.     | x < 0 = error "negative"
  6.     | x == 0 = 1
  7.     | otherwise = x * fact(x - 1)
  8.    
  9.  
  10. max2 :: Integer -> Integer -> Integer  
  11. x `max2` y
  12.     | x > y = x
  13.     | otherwise = y    
  14.  
  15. roots :: Double -> Double -> Double -> (Double, Double)
  16. roots a b c
  17.     | disc < 0 = error "no roots"
  18.     | disc == 0 = (-b/(aa), -b/(aa))
  19.     | otherwise = ((-b - sqrt(disc) )/ (aa),(-b + sqrt(disc)) / (aa))    
  20.     where disc = b^2 - 4 * a * c
  21.           aa = 2 * a  
  22.  
  23. fun :: [(Double, Double)] -> [Double]
  24. fun xs = [avg s l | (s, l) <- xs]
  25.     where avg s l = s / l
  26.  
  27. scylindr :: Double -> Double -> Double
  28. scylindr r h =
  29.     let
  30.         osns = pi * r^2
  31.         boks = 2 * pi * r * h
  32.     in
  33.         2 * osns + boks
  34.  
  35.  
  36. fun1Helper :: Int -> [Int]
  37. fun1Helper 0  = []
  38. fun1Helper n = n:fun1Helper (n - 1)
  39.  
  40. fun1 :: Int -> [Int]
  41. fun1 n = reverse(fun1Helper n)
  42.  
  43.  
  44.  
  45. fun2Helper :: Int -> [Int]
  46. fun2Helper 0 = []
  47. fun2Helper n = if odd n then n:fun2Helper(n-1) else fun2Helper(n-1)
  48.  
  49. fun2 :: Int -> [Int]
  50. fun2 n = reverse(fun2Helper n)
  51.  
  52. fun3Helper :: Int -> [Int]
  53. fun3Helper 0 = []
  54. fun3Helper n = if even n then n:fun3Helper(n-1) else fun3Helper(n-1)
  55.  
  56. fun3 :: Int -> [Int]
  57. fun3 n = reverse(fun3Helper n)
  58.  
  59. fun4H :: Int  -> [Int]
  60. fun4H 0  = []
  61. fun4H  n = (n)*(n):fun4H (n - 1)
  62.  
  63. fun4 :: Int -> [Int]
  64. fun4 n = reverse(fun4H n)
  65.  
  66. fac :: (Integral a) => a -> a
  67. fac 0 = 1
  68. fac n = n * fac(n - 1)
  69.  
  70. fun5H :: Int -> [Int]
  71. fun5H 0 = [1]
  72. fun5H n = (fac n) :fun5H(n-1)
  73.  
  74. fun5 :: Int -> [Int]
  75. fun5 n = reverse(fun5H n)
  76.  
  77. fun6H :: Int -> [Int]
  78. fun6H (-1) = []
  79. fun6H n = 2^n :fun6H (n - 1)
  80.  
  81. fun6 :: Int -> [Int]
  82. fun6 n = reverse(fun6H n)
  83.  
  84. treug :: Int -> Int
  85. treug 1 = 1
  86. treug n = n + treug (n-1)
  87.  
  88. piram :: Int -> Int
  89. piram 1 = 1
  90. piram n = treug n + piram (n-1)
  91.  
  92. fun8 :: Int -> [Int]
  93. fun8 n = [piram x | x <- [1 .. n]]
  94.  
  95.  
  96.  
  97.  
  98. avg' :: [Double] -> Double -> Double -> Double
  99. avg' [] n s = s/n
  100. avg' (x:xs) n s = avg' xs (n+1) (s+x)
  101. avg :: [Double] -> Double
  102. avg x = avg' x 0 0
  103.  
  104. removeItem :: Int -> [Int] -> [Int]
  105. removeItem n (x:xs) = if n == 1 then xs else x : removeItem (n-1) xs
  106.  
  107. addElem :: [Int] -> [Int] -> [Int]
  108. addElem [] [] = []
  109. addElem (x:xs) [] = x:xs
  110. addElem [] (y:ys) = y:ys
  111. addElem (x:xs) (y:ys) = (x + y) : addElem xs ys
  112.  
  113. swapEvenOdd :: [Int] -> [Int]
  114. swapEvenOdd [] = []
  115. swapEvenOdd [x] = [x]
  116. swapEvenOdd (x:y:xs) = y : x : swapEvenOdd xs
  117.  
  118. twopow :: Int -> Int
  119. twopow n | (n == 1) = 2
  120.         | (even n) = w * w
  121.         | otherwise = (w * w) + (w * w)
  122.         where w = twopow (n `div` 2)
  123.  
  124.  
  125. removeOdd :: [Int] -> [Int]
  126. removeOdd [] = []
  127. removeOdd (x:xs) = if odd x then removeOdd xs else x : removeOdd xs
  128.  
  129. removeEmpty :: [String] -> [String]
  130. removeEmpty [] = []
  131. removeEmpty (x:xs) = if x == "" then removeEmpty xs else x : removeEmpty xs
  132.  
  133. countTrue :: [Bool] -> Integer
  134. countTrue [] = 0
  135. countTrue (x:xs) = if x  then 1 + countTrue xs else countTrue xs
  136.  
  137. makePositive :: [Int] -> [Int]
  138. makePositive [] = []
  139. makePositive (x:xs) = if x < 0 then (-x) : makePositive xs else x : makePositive xs
  140.  
  141. delete :: Char -> String -> String
  142. delete _ "" = ""
  143. delete x (y:ys) = if x == y then delete x ys else y : delete x ys
  144.  
  145. substitute :: Char -> Char -> String -> String
  146. substitute _ _ "" = ""
  147. substitute x y (z:zs) = if x == z then y : substitute x y zs else z : substitute x y zs
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement