Advertisement
Vladi1442

Untitled

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