Advertisement
Vladi1442

Untitled

Aug 9th, 2022
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main :: IO()
  2. main = do
  3.     print $ "hello world"
  4.     print $ "Enter some text right in console!"
  5.  
  6. plus :: Int -> Int -> Int
  7. plus x y = x + y
  8.  
  9. minus :: Int -> Int -> Int
  10. minus x y = x - y
  11.  
  12. mult :: Int -> Int -> Int
  13. mult x y = x * y
  14.  
  15. divs :: Int -> Int -> Int
  16. divs x y = x / y
  17.  
  18. divs' :: Int -> Int -> Int
  19. divs' x y = div x y
  20.  
  21. mods :: Int -> Int -> Int
  22. mods x y = mod x y
  23.  
  24. addThree :: Int -> Int -> Int -> Int
  25. addThree x y z = x + y + z
  26.  
  27. factorial :: Int -> Int
  28. factorial n = product [1..n]
  29.  
  30. bigger :: Int -> Int -> Bool
  31. bigger x y = if x > y then x else y
  32.  
  33. lucky :: Int -> String
  34. lucky 7 = "You got the right number!"
  35. lucky x = "Sorry, you're out of luck!"
  36.  
  37. sayMe :: Int -> String
  38. sayMe 1 = "One"
  39. sayMe 2 = "Two"
  40. sayMe 3 = "Three"
  41. sayMe 4 = "Four"
  42. sayMe 5 = "Five"
  43. sayMe x = "Not between 1 and 5"
  44.  
  45. guessTheNumber :: Int -> String
  46. guessTheNumber 10 = "How did you do that? You got it, mate!"
  47. guessTheNumber x = "You are out of luck, sorry!"
  48.  
  49. factorial' :: Int -> Int
  50. factorial' 0 = 1
  51. factorial' 1 = 1
  52. factorial' n = n * factorial (n-1)
  53.  
  54. charName :: Char -> String
  55. charName 'a' = "Albert"
  56. charName 'b' = "Broseph"
  57. charName 'c' = "Cercil"
  58.  
  59. addVectors1 :: (Ord a, Num a) => (a,a) -> (a,a) -> (a,a)
  60. addVectors1 x y = (fst x + fst y, snd x + snd y)
  61.  
  62. addVectors2 :: (Ord a, Num a) => (a,a) -> (a,a) -> (a,a)
  63. addVectors2 (x1,y1) (x2,y2) = (x1+x2,y1+y2)
  64.  
  65. first :: (a,b,c) -> a
  66. first (x, _, _) = x
  67.  
  68. second :: (a,b,c) -> b
  69. second (_, y, _) = y
  70.  
  71. third :: (a,b,c) -> c
  72. third (_, _, z) = z
  73.  
  74. head :: [Int] -> Int
  75. head [] = error "Can't call head on empty list, dummy"
  76. head (x:_) = x
  77.  
  78. -- here we don't care about our first argument
  79.  
  80. length :: [Int] -> Int
  81. length [] = 0
  82. length (_:xs) = 1 + length xs
  83.  
  84. -- but here we are interested in our first argument
  85.  
  86. sum :: [Int] -> Int
  87. sum [] = 0
  88. sum (x:xs) = x + sum xs
  89.  
  90. bmiTell :: (RealFloat a) => a -> String
  91. bmiTell bmi
  92.     | bmi <= 18.5 = "You are underweight"
  93.     | bmi <= 25.0 = "You are normal weight"
  94.     | bmi <= 30.0 = "You are overweight"
  95.     | otherwise = "If you contunue to eat, you are gonna die"
  96.  
  97. bmiTell1 :: (RealFloat a) => a -> a -> String
  98. bmiTell1 weight height
  99.     | weight / height ^ 2 <= 18.5 = "You are underweight"
  100.     | weight / height ^ 2 <= 25.0 = "You are normal weight"
  101.     | weight / height ^ 2 <= 30.0 = "You are overweight"
  102.     | otherwise = "You are a whale, congratulations"
  103.  
  104. bmiTell2 :: (RealFloat a) => a -> a -> String
  105. bmiTell2 weight height
  106.     | bmi <= 18.5 = "You are underweight"
  107.     | bmi <= 25.0 = "You are normal weight"
  108.     | bmi <= 30.0 = "You are overweight"
  109.     | otherwise = "You are a whale, congratulations"
  110.         where
  111.             bmi = weight / height ^ 2
  112.  
  113. bmiTell3 :: (RealFloat a) => a -> a -> String
  114. bmiTell3 weight height
  115.     | bmi <= skinny = "You are underweight"
  116.     | bmi <= normal = "You are normal weight"
  117.     | bmi <= overweight = "You are overweight"
  118.     | otherwise = "You are a whale, congratulations"
  119.  
  120. max1 :: Int -> Int -> Int
  121. max1 a b
  122.     | a > b = a
  123.     | otherwise = b
  124.  
  125. min1 :: Int -> Int -> Int
  126. min1 a b
  127.     | a > b = b
  128.     | otherwise = a
  129.    
  130. maxThree :: Int -> Int -> Int -> Int
  131. maxThree a b c = if (a > b) && (a > c) then a else if (b > a) && (b > c) then b else c
  132.  
  133. maxThree1 :: Int -> Int -> Int -> Int
  134. maxThree1 a b c
  135.     | (a > b) && (a > c) = a
  136.     | (b > a) && (b > c) = b
  137.     | otherwise = c
  138.  
  139. myCompare :: Int -> Int -> String
  140. myCompare a b
  141.     | a > b = "GT"
  142.     | a < b = "LT"
  143.     | otherwise = "EQ"
  144.  
  145. lastDigit :: Int -> Int
  146. lastDigit n = mod n 10
  147.  
  148. removeLastDigit :: Int -> Int
  149. removeLastDigit n = div n 10
  150.  
  151. areNotEqualOneLine :: Int -> Int -> Bool
  152. areNotEqualOneLine a b = a == b
  153.  
  154. areNotEqualGuards :: Int -> Int -> Bool
  155. areNotEqualGuards a b
  156.     | a == b = True
  157.     | otherwise = False
  158.  
  159. inside :: Int -> Int -> Int -> Bool
  160. inside a b x
  161.     | (x >= a) && (x <= b) = True
  162.     | otherwise = False
  163.    
  164. factoriel :: Int -> Int
  165. factoriel 0 = 1
  166. factoriel 1 = 1
  167. factoriel n = n * factoriel (n-1)
  168.  
  169. factIter :: Int -> Int
  170. factIter n = helper n 1
  171.     where
  172.         helper :: Int -> Int -> Int
  173.         helper 0 result = result
  174.         helper leftOver result = helper (leftOver - 1) (result*leftOver)
  175.  
  176. fibonacci :: Int -> Int
  177. fibonacci 0 = 0
  178. fibonacci 1 = 1
  179. fibonacci n = fibonacci (n-1) + fibonacci (n-2)
  180.  
  181. fibIter :: Int -> Int -> Int
  182. fibIter n = helper n 1 0
  183.     where
  184.         helper :: Int -> Int -> Int -> Int
  185.         helper 0 n1 _ = n1
  186.         helper 1 _ n2 = n2
  187.         helper n n1 n2 = helper (n-1) n2 (n2+n1)
  188.  
  189. reverse :: Int -> Int
  190. reverse 0 = 0
  191. reverse n = (mod n 10) * 10 + reverse (div n 10)
  192.  
  193. isPalindrome :: Int -> Bool
  194. isPalindrome n = n == reverse n
  195.  
  196. sumDigitRec :: Int -> Int
  197. sumDigitRec n = (mod n 10) + sumDigitRec (div n 10)
  198.  
  199. powRec :: Int -> Int -> Int
  200. powRec _ 0 = 1
  201. powRec n x = x * powRec (n-1)
  202.  
  203. isPrime :: Int -> Bool
  204. isPrime n = n > 1 && helper 2
  205.     where
  206.         helper :: Int -> Bool
  207.         helper i
  208.             | (fromIntegral i) >= (sqrt $ fromIntegral n) = True
  209.             | mod n i == 0 = False
  210.             | otherwise = helper (i+1)
  211.  
  212. sumDivs :: Int -> Int
  213. sumDivs n = helper 1 0
  214.     where
  215.         helper currentDiv
  216.             | currentDiv >= n = n
  217.             | mod n currentDiv == 0 = currentDiv + helper (currentDiv+1)
  218.             | otherwise = helper (currentDiv+1)
  219.            
  220. hasIncDigits :: Int -> Bool
  221. hasIncDigits n = (n < 10) || mod n 10 >= mod (div n 10) 10 && hasIncDigits (div n 10)
  222.  
  223. countDigits :: Int -> Int
  224. countDigits n
  225.     | n < 0 = error "Nope"
  226.     | n < 10 = 1
  227.     | otherwise = 1 + countDigits (div n 10)
  228.    
  229. isNarcisticc :: Int -> Bool
  230. isNarcisticc n = n == helper n (countDigits n)
  231.     where
  232.         helper :: Int -> Int -> Int
  233.         helper 0 nd = 0
  234.         helper leftover nd = (mod leftover 10)^nd + helper (div leftover 10) nd
  235.  
  236. calculateSum :: Int -> Int -> Int
  237. calculateSum x n = helper 0 0
  238.     where
  239.         helper sumOfDegree degree
  240.             | n <= degree = sumOfDegree + x^degree
  241.             | otherwise = helper (sumOfDegree+x^degree) (degree+1)
  242.  
  243. findMax :: Int -> Int
  244. findMax n = helper n 0
  245.     where
  246.         helper :: Int -> Int -> Int
  247.         helper 0 currMax = currMax
  248.         helper num currMax
  249.          | (mod num 10) > currMax = helper (div num 10) (mod num 10)
  250.          | otherwise = helper (div num 10) currMax
  251.  
  252. sumNumbers :: Int -> Int -> Int
  253. sumNumbers start finish = helper $ min start finish
  254. --  | start > finish = sumNumbers finish start
  255. --  | otherwise = helper start
  256.  where
  257.      helper :: Int -> Int
  258.      helper i
  259.       | i > max start finish = 0
  260.       | hasDecDigits i = i + helper (i+1)
  261.       | otherwise = helper (i+1)
  262.  
  263. hasDecDigits :: Int -> Bool
  264. hasDecDigits n = n < 10 || mod n 10 <= mod (div n 10) 10 && hasDecDigits (div n 10)
  265.  
  266. subNum :: Int -> Int -> Bool
  267. subNum x y = helper y (10^countDigits x)
  268.  where
  269.   helper :: Int -> Int -> Bool
  270.   helper y dNum
  271.    | x > y = False
  272.    | mod y dNum == x = True
  273.    |otherwise = helper (div y 10) dNum
  274.  
  275. digitalRoot :: Int -> Int
  276. digitalRoot n
  277.  | n <= 9 = n
  278.  | otherwise = digitalRoot (sumDigits n)
  279.  
  280. sumDigits :: Int -> Int
  281. sumDigits n
  282.    | div n 10 == 0 = n
  283.    | otherwise = mod n 10 + sumDigits (div n 10)
  284.    
  285. mySin :: Integer -> Double -> Double
  286. mySin 0 x = x
  287. mySin n x = ((-1)^n * x^(2*n + 1)) / (fromIntegral $ factoriel $ 2*n + 1) + mySin (n - 1) x
  288.  
  289.  
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement