Advertisement
Vladi1442

Untitled

Aug 9th, 2022
444
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 n = (mod n 10)*10 + reverseInteger (div n 10)
  192.  
  193. isPalindrome :: Int -> Bool
  194. isPalindrome n = n == reverseInteger n
  195.  
  196. sumDigitsRec :: Int -> Int
  197. sumDigitsRec n = mod n 10 + sumDigitsRec (div n 10)
  198.  
  199. powRec :: Int -> Int -> Int
  200. powRec _ n = 1
  201. powRec x n = x * powRec (n-1)
  202.  
  203. isPrime :: Int -> Bool
  204. isPrime 1 = False
  205. isPrime 2 = True
  206. isPrime n = n >= 1 && helper 2
  207.     where
  208.         helper :: Int -> Bool
  209.         helper i
  210.             | (fromIntegral i) >= (sqrt $ fromIntegral n) = True
  211.             | mod n i == 0 = False
  212.             | otherwise = helper (i+1)
  213.  
  214. sumDivs :: Int -> Int
  215. sumDivs n = helper 1
  216.     where
  217.         helper :: Int -> Int
  218.         helper currentDiv
  219.             | currentDiv >= n = n
  220.             | mod n currentDiv == 0 = currentDiv + helper (currentDiv+1)
  221.             | otherwise = helper (currentDiv+1)
  222.  
  223. isPerfect :: Int -> Bool
  224. isPerfect n = n == sumDivs n
  225.  
  226. hasIncDigits :: Int -> Bool
  227. hasIncDigits 0 = False
  228. hasIncDigits n = (n < 10) || mod n 10 >= mod (div n 10) 10 && hasIncDigits (div n 10)
  229.  
  230. -- Recursion 2
  231.  
  232. numDig :: Int -> Int
  233. numDig n
  234.     | n < 0 = error "n was negative"
  235.     | n < 10 = 1
  236.     | otherwise = 1 + numDig (div n 10)
  237.  
  238. isNarcisticc :: Int -> Bool
  239. isNarcisticc n = n == helper n (numDig n)
  240.     where
  241.         helper :: Int -> Int -> Int
  242.         helper 0 nd = 0
  243.         helper leftover nd = (mod leftover 10)^nd + helper (div leftover 10) nd    
  244.  
  245. calculateSum :: Int -> Int -> Int
  246. calculateSum x n = helper 0 0
  247.     where
  248.         helper :: Int -> Int -> Int
  249.         helper sumOfDegree degree
  250.             | n <= degree = sumOfDegree + x ^ degree
  251.             | otherwise = helper (sumOfDegree + x ^ degree) (degree+1)
  252.  
  253. findMax :: Int -> Int
  254. findMax n = helper n 0
  255.     where
  256.         helper :: Int -> Int -> Int
  257.         helper 0 currMax = currMax
  258.         helper num currMax
  259.             | (mod num 10) > currMax = helper (div num 10) (mod num 10)
  260.             | otherwise = helper (div num 10) currMax
  261.  
  262. sumNumbers :: Int -> Int -> Int
  263. sumNumbers start finish = helper $ min start finish
  264.     where
  265.          helper :: Int -> Int
  266.          helper i
  267.           | i > max start finish = 0
  268.           | hasDecDigits i = i + helper (i+1)
  269.           | otherwise = helper (i+1)
  270.          
  271. -- actually it is equivalent with hasIncDigits, the only difference is the sign (<=)
  272.  
  273. hasDecDigits :: Int -> Bool
  274. hasDecDigits n = n < 10 || mod n 10 <= mod (div n 10) 10 && hasDecDigits (div n 10)
  275.  
  276. subNum :: Int -> Int -> Bool
  277. subNum x y = helper y (10^countDigits x)
  278.     where
  279.         helper :: Int -> Int -> Bool
  280.         helper y dNum
  281.             | x > y = False
  282.             | mod y dNum == x = True
  283.             |otherwise = helper (div y 10) dNum
  284.  
  285. digitalRoot :: Int -> Int
  286. digitalRoot n
  287.  | n <= 9 = n
  288.  | otherwise = digitalRoot (sumDigits n)
  289.  
  290. sumDigits :: Int -> Int
  291. sumDigits n
  292.    | div n 10 == 0 = n
  293.    | otherwise = mod n 10 + sumDigits (div n 10)
  294.    
  295. mySin :: Integer -> Double -> Double
  296. mySin 0 x = x
  297. mySin n x = ((-1)^n * x^(2*n + 1)) / (fromIntegral $ factoriel (2*n + 1)) + mySin (n - 1) x
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement