Zorikto

lab2hask

Apr 11th, 2021 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. module Lab2 where
  2.  
  3. -- 1
  4. fun1 :: Int -> [Int]
  5. fun1 0 = []
  6. fun1 x = fun1 (x-1) ++ (x : [])
  7.  
  8. fun2 :: Int -> [Int]
  9. fun2 0 = []
  10. fun2 x = fun2 (x-1) ++ (2*x-1 : [])
  11.  
  12. fun3 :: Int -> [Int]
  13. fun3 0 = []
  14. fun3 x = fun3 (x-1) ++ (2*x : [])
  15.  
  16. fun4 :: Int -> [Int]
  17. fun4 0 = []
  18. fun4 x = fun4 (x-1) ++ (x*x : [])
  19.  
  20. fac :: (Integral a) => a -> a
  21. fac n = product [1..n]
  22. fun5 :: Int -> [Int]
  23. fun5 0 = []
  24. fun5 x = fun5 (x-1) ++ (fac(x) : [])
  25.  
  26. fun6 :: Int -> [Int]
  27. fun6 0 = []
  28. fun6 x = fun6 (x-1) ++ (2 ^ (x - 1) : [])
  29.  
  30. treug :: Int -> Int
  31. treug 1 = 1
  32. treug n = n + treug (n-1)
  33. fun7 :: Int -> [Int]
  34. fun7 0 = []
  35. fun7 x = fun7 (x-1) ++ (treug x : [])
  36.  
  37. piram :: Int -> Int
  38. piram 1 = 1
  39. piram n = treug n + piram (n-1)
  40. fun8 :: Int -> [Int]
  41. fun8 0 = []
  42. fun8 x = fun8 (x-1) ++ (piram (x) : [])
  43.  
  44. -- 2
  45. avg' :: [Double] -> Double -> Double -> Double
  46. avg' [] n s = s/n
  47. avg' (x:xs) n s = avg' xs (n+1) (s+x)
  48. avg :: [Double] -> Double
  49. avg x = avg' x 0 0
  50.  
  51. removeItem :: Int -> [Int] -> [Int]
  52. removeItem n (x:xs) = if n == 1 then xs else x : removeItem (n-1) xs
  53.  
  54. addElem :: [Int] -> [Int] -> [Int]
  55. addElem [] [] = []
  56. addElem (x:xs) [] = x:xs
  57. addElem [] (y:ys) = y:ys
  58. addElem (x:xs) (y:ys) = (x + y) : addElem xs ys
  59.  
  60.  
  61. swapEvenOdd :: [Int] -> [Int]
  62. swapEvenOdd [] = []
  63. swapEvenOdd [x] = [x]
  64. swapEvenOdd (x:y:xs) = if (odd(x + y)) then y :swapEvenOdd (x : xs) else x : swapEvenOdd (y : xs)
  65.  
  66.  
  67. twopow :: Integer -> Integer
  68. twopow n = if n == 1 then 2 else if even n then twopow (div n 2) * twopow (div n 2) else twopow (div n 2) * twopow (div n 2) * 2
  69.  
  70. isOdd:: Int -> Bool
  71. isOdd x = mod x 2 /= 0
  72. removeOdd :: [Int] -> [Int]
  73. removeOdd [] = []
  74. removeOdd (x:xs) = if isOdd x then removeOdd xs else x : removeOdd xs
  75.  
  76. removeEmpty :: [String] -> [String]
  77. removeEmpty [] = []
  78. removeEmpty (x:xs) = if x == "" then removeEmpty xs else x : removeEmpty xs
  79.  
  80. countTrue :: [Bool] -> Int
  81. countTrue [] = 0
  82. countTrue (x:xs) = if x == True then 1 + countTrue xs else countTrue xs
  83.  
  84. makePositive :: [Int] -> [Int]
  85. makePositive [] = []
  86. makePositive (x:xs) = if x < 0 then (-x) : makePositive xs else x : makePositive xs
  87.  
  88. delete :: Char -> String -> String
  89. delete _ "" = ""
  90. delete x (y:ys) = if x == y then delete x ys else y : delete x ys
  91.  
  92. substitute :: Char -> Char -> String -> String
  93. substitute _ _ "" = ""
  94. substitute x y (z:zs) = if x == z then y : substitute x y zs else z : substitute x y zs
Add Comment
Please, Sign In to add comment