Advertisement
Vladi1442

Untitled

Jul 1st, 2022
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import Data.Char
  3.  
  4. main :: IO()
  5. main = do
  6.  
  7. -- 1.
  8.    
  9. hasElemets :: [Int] -> Bool
  10. hasElementsPM [] = False
  11. hasElements xs = not null xs
  12.  
  13. hasElements :: [Int] -> Bool
  14. hasElementsPM [] = False
  15. hasElementsPM xs = length xs /= 0
  16.  
  17. hasElements :: [Int] -> Bool
  18. hasElementsPM [] = False
  19. hasElementsPM xs = xs /= []
  20.  
  21. -- 2.
  22.  
  23. myLengthRecPM :: [Int] -> Int
  24. myLengthRecPM [] = 0
  25. myLengthRecPM (_:xs) = 1 + myLengthRecNonPM xs
  26.  
  27. myLengthRecNonPM :: [Int] -> Int
  28. myLengthRecNonPM [] = 0
  29. myLengthRecNonPM xs
  30.     | null xs = 0
  31.     | otherwise = 1 + (myLengthRecNonPM $ tail xs)
  32.  
  33. -- 3.
  34.  
  35. getClosedInterval :: Int -> Int -> [Int]
  36. getClosedInterval x y = [min x y .. max x y]
  37.  
  38. -- 4.
  39.  
  40. isInside :: Int -> Int -> Int -> Bool
  41. isInside x y n = elem n [min x y .. max x y]
  42.  
  43. -- 5.
  44.  
  45. removeFirst :: Int -> [Int] -> [Int]
  46. removeFirst 0 [] = []
  47. removeFirst (x:xs)
  48.     | n == x = removeFirst n xs
  49.     | otherwise = removeFirst n xs
  50.  
  51. -- 6.
  52.  
  53. removeAllRec :: Int -> [Int] -> [Int]
  54. removeAllRec 0 [] = []
  55. removeAllRec (x:xs)
  56.     | n == x = removeAllRec n xs
  57.     | otherwise = x : removeAllRec n xs
  58.  
  59. removeAllHOF :: Int -> [Int] -> [Int]
  60. removeAllHOF 0 [] = []
  61. removeAllHOF n xs = filter (\x -> x /= n) xs
  62.  
  63. -- 7.
  64.  
  65. incrementByLC :: Int -> [Int] -> [Int]
  66. incrementByLC 0 [] = []
  67. incrementByLC n xs = [n + x | x <- xs]
  68.  
  69. incrementByHOF :: Int -> [Int] -> [Int]
  70. incrementByHOF 0 [] = []
  71. incrementByHOF n xs = map (+n) xs
  72.  
  73. -- 8.
  74.  
  75. rev n = read $ reverse $ show n
  76.  
  77. isPrime n = (n >= 1) && [1,n] == [d | d <- [1.. n], mod n d == 0]
  78.  
  79. isPrime' n = (n > 2) && all (\i -> mod n i /= 0) [2.. n-1]
  80.  
  81. sumDivs n = sum [d | d <- [1..n], mod n d == 0]
  82.  
  83. sumDig n = sum $ map digitToInt $ show n
  84.  
  85. -- 9.
  86.  
  87. getPrimesLC :: Int -> Int -> [Int]
  88. getPrimesLC x y = [d | d <- [min x y .. max x y], isPrime d && (elem '7' $ show d)]
  89.  
  90. getPrimesHOF :: Int -> Int -> [Int]
  91. getPrimesHOF x y = filter (\d -> isPrime d && (elem '7' $ show d)) [min x y .. max x y]
  92.  
  93. -- 10.
  94.  
  95. mySumPM :: [Int] -> Int
  96. mySumPM [] = 0
  97. mySumPM (x:xs) = x + mySumPM xs
  98.  
  99. mySumNonPM :: [Int] -> Int
  100. mySumNonPM [] = 0
  101. mySumNonPM (x:xs)
  102.    | null xs = 0
  103.    | otherwise = x + mySumNonPM xs
  104.  
  105. -- 11.
  106.  
  107. isPresent :: Int -> [Int] -> Bool
  108. isPresent 0 [] = False
  109. isPresent n xs
  110.    | elem n xs == True
  111.    | otherwise = False
  112.  
  113. -- 12.
  114.  
  115. primesInRangeLC :: Int -> Int -> [Int]
  116. primesInRangeLC x y = [d | d <- [min x y .. max x y], isPrime d && d >= 3]
  117.  
  118. primesInRangeHOF :: Int -> Int -> [Int]
  119. primesInRangeHOF x y = filter (\d -> isPrime d && d >= 3) [min x y .. max x y]
  120.  
  121. -- 13.
  122.  
  123. sumUnevenLC :: Int -> Int -> Int
  124. sumUnevenLC x y = sum [d | d <- [x .. y], mod d 2 /= 0]
  125.  
  126. sumUnevenHOF :: Int -> Int -> Int
  127. sumUnevenHOF x y = sum $ filter (\d -> mod d 2 /= 0) [x .. y]
  128.  
  129. -- 14.
  130.  
  131. isAscending :: Int -> Int -> Int
  132. isAscending n = ((mod n 10) < 10) && (mod n 10) >= (mod (div n 10) 10) && isAscending (div n 10)
  133.  
  134. -- 15.
  135.  
  136. sumSpecialPrimes :: Int -> Int -> Int
  137. sumSpecialPrimes n d = sum $ take n $ filter (\x -> isPrime x && ((elem n) (intToDigit d)) [1..]
  138.  
  139. -- 16.
  140.  
  141. myLambda :: (a -> a) -> (a -> a)
  142. myLambda f = (\x -> f x)
  143.  
  144. negatePred :: (a -> Bool) -> (a -> Bool)
  145. negatePred p = (\x -> not (p x))
  146.  
  147. compose :: (c -> a) -> (b -> c) -> (b -> a)
  148. compose f g = (\x -> f (g x))
  149.  
  150. partiallyApply :: (a -> a) -> a -> (a -> a)
  151. partiallyApply f x = (\y -> f x y)
  152.  
  153. -- 17.
  154.  
  155. difference :: (a -> a) -> (a -> a)
  156. difference f = (\(x,y) -> f y - f x)
  157.  
  158. -- 18.
  159.  
  160. upperBound :: (a -> a) -> a -> (a -> a)
  161. upperBound f y = (\x -> max (f x) y)
  162.  
  163. -- 19.
  164.  
  165. sumTuplePM :: (Int, Int) -> Int
  166. sumTuplePM (x,y) = x + y
  167.  
  168. sumTupleNonPM :: (Int, Int) -> Int
  169. sumTupleNonPM vec = fst vec + snd vec
  170.  
  171. -- 20.
  172.  
  173. type Point = (Int, Int)
  174.  
  175. dividePM :: Point -> Point
  176. dividePM (x,y) = (div x y, mod x y)
  177.  
  178. divideNonPM :: Point -> Point
  179. divideNonPM vec = (div (fst vec) (snd vec), mod (fst vec) (snd vec))
  180.  
  181. -- 21.
  182.  
  183. type Rat = (Int, Int)
  184.  
  185. normalizeUsingLet :: Rat -> Rat
  186. normalizeUsingLet (x,y) = let g = gcd x y = (div x d, div y d)
  187.  
  188. normalize :: Rat -> Rat
  189. normalize (x,y) = (div x d, div y d)
  190.    where d = gcd x y
  191.  
  192. -- 22.
  193.  
  194. getSquares :: Int -> Int -> Int -> [(Int, Int)]
  195. getSquares s f k = [(x, x*x) | x <- [s, s+k .. f]]
  196.  
  197. -- 23.
  198.  
  199. type Vector a = (a,a,a)
  200.  
  201. sumVectors :: Vector a -> Vector a -> Vector a
  202. sumVectors (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
  203.  
  204. scaleVector :: Vector a -> a -> Vector a
  205. scaleVector (x,y,z) l = (l*x, l*y, l*z)
  206.  
  207. -- 24.
  208.  
  209. rev n = read $ reverse $ show n
  210.  
  211. isPalindrome :: Int -> Bool
  212. isPalindrome n = n == rev n
  213.  
  214. getDivs :: Int -> Int
  215. getDivs n = [d | d <- [1..n], mod n d == 0]
  216.  
  217. getPalindromes :: Int -> Int
  218. getPalindromes n = minimum s + maximum s
  219.    where s = getDivs n
  220.    
  221. -- 25.
  222.  
  223. fS :: Int -> Int
  224. fS n = mod n 10
  225.  
  226. sS :: Int -> Int
  227. sS n = mod (div n 10) 10
  228.  
  229. tS :: Int -> Int
  230. tS n = mod (div n 100) 10
  231.  
  232. fS :: Int -> Int
  233. fS n = mod (div n 1000) 10
  234.  
  235. isArithmetic :: [Int] -> Bool
  236. isArithmetic [] = False
  237. isArithmetic xs
  238.    | fS xs == sS xs == tS xs == fS xs = True
  239.    | otherwise = False
  240.    
  241. -- 26.    
  242.  
  243. specialSum :: Int -> Int -> Int
  244. specialSum x y = sum [d | d <- [x ..y], (elem '6' $ show d) && (mod (d-1) 4 == 0]
  245.  
  246. -- 27.
  247.  
  248. applyN :: (a -> a) -> a
  249. applyN n = (\x -> x * n)
  250.  
  251. -- 28.
  252.  
  253. type Vector a = (a,a,a)
  254.  
  255. dotProduct :: Vector a -> Vector a -> a
  256. dotProduct (x1,x2,x3) (y1,y2,y3) = [(x1*y1) + (x2*y2) + (x3*y3) | ((x1,x2,x3) (y1,y2,y3) <- zip (x1,x2,x3) (y1,y2,y3))]
  257.  
  258. crossProduct :: Vector a -> Vector a -> Vector a
  259. crossProduct (x1, x2, x3) (y1, y2, y3) = sum [ (i*x2y3) + (j*x3*y1) + (k*x1y2) - (y1x2*k) - (x3*y2*i) - (x1*y3*j) | ((i,j,k),(x1,x2,x3),(y1,y2,y3)) <- zip (i,j,k) (x1,x2,x3) (y1,y2,y3)]
  260.  
  261. magnitude :: Vector a -> Double
  262. magnitude (x,y,z) = [sqrt ((x*x) + (y*y) + (z*z)) | (x,y,z) <- zip (x,y,z)]
  263.  
  264. -- 28.
  265.  
  266. myPolynomial :: (a -> a) -> [a] -> (a -> a)
  267. myPolynomial f ys = (\x -> sum [n - f(y*n) | (y,n) <- zip ys [1..]])
  268.  
  269. -- 29.
  270.  
  271. dominates :: (a -> a) -> (a -> a) -> [a] -> Bool
  272. dominates f g = all (\x -> f x >= g x)
  273.  
  274. dominates :: (a -> a) -> (a -> a) -> [a] -> Bool
  275. dominates f g xs = foldl (\acc x -> acc && f x >= g x) True xs
  276.  
  277. isInteresing :: Int -> Bool
  278. isInteresting n = mod n (sum $ map digitToInt $ show n) == 0
  279.  
  280. mySin :: Int -> Int -> Double
  281. mySin 0 x = 0
  282. mySin n x = (((-1)^n * x^(2*n + 1)) / (product $ fromIntegral [1 .. 2*n + 1])) + mySin (n-1) x
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement