vencinachev

Day3FP

Nov 21st, 2021 (edited)
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sumMy :: (Num a) => [a] -> a
  2. sumMy [] = 0
  3. sumMy (x:xs) = x + sumMy xs
  4.  
  5. headMy :: [a] -> a
  6. headMy (x:_) = x
  7.  
  8. tailMy :: [a] -> [a]
  9. tailMy (_:xs) = xs
  10.  
  11. -- reverseMy :: [a] -> [a]
  12. reverseMy [] = []
  13. reverseMy list = last list : reverseMy (init list)
  14.  
  15. reverseMy1 :: [a] -> [a]
  16. reverseMy1 [] = []
  17. reverseMy1 (x:xs) = reverseMy1 xs ++ [x]
  18.  
  19. len :: [a] -> Int
  20. len [] = 0
  21. len (_:xs) = 1 + len xs
  22.  
  23. count :: (Ord a) => a -> [a] -> Int
  24. count _ [] = 0
  25. count num (x:xs)
  26.  | num == x  = 1 + count num xs
  27.  | otherwise = count num xs
  28.  
  29.  
  30. triangle :: Float -> Float -> Float -> [Float]
  31. triangle a b c
  32.  | a>0 && b>0 && c>0 && a<b+c && b<a+c && c<a+b = [per, s]
  33.  | otherwise = [-1, -1]
  34.  where
  35.  per = a + b + c
  36.  p = per/2
  37.  s = sqrt (p*(p-a)*(p-b)*(p-c))
  38.  
  39. rect :: Float -> Float -> (Float, Float, [Char])
  40. rect a b
  41.  | a <= 0 || b <= 0 = (-1, -1, "invalid")
  42.  | a == b           = (p, s, "square")
  43.  | otherwise        = (p, s, "rectangle")
  44.  where
  45.  p = 2*(a+b)
  46.  s = a*b
  47.  
  48. doubleNum x = 2*x
  49.  
  50.  
  51. mapMy :: (a->b) -> [a] -> [b]
  52. mapMy _ [] = []
  53. mapMy f (x:xs) = f x : mapMy f xs
  54.  
  55.  
  56. qSort :: (Ord a) => [a] -> [a]
  57. qSort [] = []
  58. qSort (x:xs) = qSort less ++ [x] ++ qSort more
  59.  where
  60.   less = (filter (<x) xs)
  61.   more = (filter (>=x) xs)
  62.  
  63. qSort2 :: (Ord a) => [a] -> [a]
  64. qSort2 [] = []
  65. qSort2 (x:xs) = qSort2 [y | y <- xs, y < x] ++ [x] ++ qSort2 [y | y <- xs, y >= x]
  66.  
  67. primes :: [Int]
  68. primes = filterPrime [2..]
  69.   where filterPrime (p:xs) =
  70.           p : filterPrime [x | x <- xs, x `mod` p /= 0]
  71.  
  72.  
  73. getOperation :: Int -> (Int->Int->Int)
  74. getOperation 1 = \x y -> x + y
  75. getOperation 2 = \x y -> x - y
  76. getOperation 3 = \x y -> x * y
  77. getOperation 4 = \x y -> x `div` y
  78.  
  79.  
  80.  
Add Comment
Please, Sign In to add comment