Advertisement
O_Egor

Haskell laba 1

Apr 25th, 2021 (edited)
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. fun1 :: Int -> Int -> [Int]
  37. fun1 0 n = []
  38. fun1 k n = (n - k + 1):fun1 (k - 1) n
  39.  
  40.  
  41. isOdd:: Int -> Bool
  42. isOdd x = mod x 2 /= 0
  43.  
  44. fun2Helper :: Int -> [Int]
  45. fun2Helper 0 = []
  46. fun2Helper n = if isOdd n then n:fun2Helper(n-1) else fun2Helper(n-1)
  47.  
  48. fun2 :: Int -> [Int]
  49. fun2 n = reverse(fun2Helper n)
  50.  
  51. fun3Helper :: Int -> [Int]
  52. fun3Helper 0 = []
  53. fun3Helper n = if isOdd n then fun3Helper(n-1) else n:fun3Helper(n-1)
  54.  
  55. fun3 :: Int -> [Int]
  56. fun3 n = reverse(fun3Helper n)
  57.  
  58. fun4 :: Int -> Int -> [Int]
  59. fun4 0 n = []
  60. fun4 k n = (n - k + 1)*(n - k + 1):fun4 (k - 1) n
  61.  
  62. fac :: (Integral a) => a -> a
  63. fac 0 = 1
  64. fac n = n * fac(n - 1)
  65.  
  66. fun5 :: Int -> [Int]
  67. fun5 n = [fac x | x <- [1 .. n]]
  68.  
  69. fun6H :: Int -> [Int]
  70. fun6H (-1) = []
  71. fun6H n = 2^n :fun6H (n - 1)
  72.  
  73. fun6 :: Int -> [Int]
  74. fun6 n = reverse(fun6H n)
  75.  
  76. treug :: Int -> Int
  77. treug 1 = 1
  78. treug n = n + treug (n-1)
  79.  
  80. piram :: Int -> Int
  81. piram 1 = 1
  82. piram n = treug n + piram (n-1)
  83.  
  84. fun8 :: Int -> [Int]
  85. fun8 n = [piram x | x <- [1 .. n]]
  86.  
  87.  
  88.  
  89.  
  90. avg' :: [Double] -> Double -> Double -> Double
  91. avg' [] n s = s/n
  92. avg' (x:xs) n s = avg' xs (n+1) (s+x)
  93. avg :: [Double] -> Double
  94. avg x = avg' x 0 0
  95.  
  96. removeItem :: Int -> [Int] -> [Int]
  97. removeItem n (x:xs) = if n == 1 then xs else x : removeItem (n-1) xs
  98.  
  99. addElem :: [Int] -> [Int] -> [Int]
  100. addElem [] [] = []
  101. addElem (x:xs) [] = x:xs
  102. addElem [] (y:ys) = y:ys
  103. addElem (x:xs) (y:ys) = (x + y) : addElem xs ys
  104.  
  105. swapEvenOdd :: [Int] -> [Int]
  106. swapEvenOdd [] = []
  107. swapEvenOdd [x] = [x]
  108. swapEvenOdd (x:y:xs) = y : x : swapEvenOdd xs
  109.  
  110. twopow :: Int -> Int
  111. twopow n | (n == 1) = 2
  112.         | (even n) = w * w
  113.         | otherwise = (w * w) + (w * w)
  114.         where w = twopow (n `div` 2)
  115.  
  116. isOdd:: Int -> Bool
  117. isOdd x = mod x 2 /= 0
  118. removeOdd :: [Int] -> [Int]
  119. removeOdd [] = []
  120. removeOdd (x:xs) = if isOdd x then removeOdd xs else x : removeOdd xs
  121.  
  122. removeEmpty :: [String] -> [String]
  123. removeEmpty [] = []
  124. removeEmpty (x:xs) = if x == "" then removeEmpty xs else x : removeEmpty xs
  125.  
  126. countTrue :: [Bool] -> Integer
  127. countTrue [] = 0
  128. countTrue (x:xs) = if x == True then 1 + countTrue xs else countTrue xs
  129.  
  130. makePositive :: [Int] -> [Int]
  131. makePositive [] = []
  132. makePositive (x:xs) = if x < 0 then (-x) : makePositive xs else x : makePositive xs
  133.  
  134. delete :: Char -> String -> String
  135. delete _ "" = ""
  136. delete x (y:ys) = if x == y then delete x ys else y : delete x ys
  137.  
  138. substitute :: Char -> Char -> String -> String
  139. substitute _ _ "" = ""
  140. substitute x y (z:zs) = if x == z then y : substitute x y zs else z : substitute x y zs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement