Advertisement
vencinachev

Day3FP

Nov 8th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Char
  2.  
  3. a = [2*n | n <- [1,2,3,4]]
  4. b = [2*n-1 | n <- [1,2,3,4]]
  5. c = [even n | n <- [2, 4, 7, 8]]
  6. d = [odd n | n <- [2, 4, 7, 8]]
  7. e = [2*n | n <- [2, 4, 5, 7, 8], even n, n > 3]
  8. f = [2^n | n <- [21, 14, 5, 7, 8], odd n, n > 6]
  9. g = [sqrt n | n <- [1..20]]
  10. h = [(x, y) | x <- [1, 2, 3], y <- ['a', 'b', 'c', 'd']]
  11.  
  12. sumPairs :: [(Int, Int)] -> [Int]
  13. sumPairs pairsList = [x + y | (x, y) <- pairsList]
  14.  
  15. isDigit :: Char -> Bool
  16. isDigit ch = ch >= '0' && ch <= '9'
  17.  
  18. isLetter :: Char -> Bool
  19. isLetter letter = (letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')
  20.  
  21. allDigits :: String -> (Char -> Bool) -> String
  22. allDigits str option = [ch | ch <- str, option ch]
  23.  
  24.  
  25. toUpperMy :: Char -> Char
  26. toUpperMy ch = chr  (ord ch + ord 'A' - ord 'a')
  27.  
  28. exor :: Bool -> Bool -> Bool
  29. exor x y = (x && not y) || (not x && y)
  30.  
  31. filterMy :: (Int->Bool) -> [Int] -> [Int]
  32. filterMy p [] = []
  33. filterMy p (x:xs)
  34.    | p x             = x : filterMy p xs
  35.    | otherwise = filterMy p xs
  36.  
  37. {-
  38. mapMy :: (a -> b) -> [a] -> [b]
  39. mapMy f []       = []
  40. mapMy f (x:xs) = (f x) : (map f xs)
  41. -}
  42.  
  43. mapMy :: (a -> b) -> [a] -> [b]
  44. mapMy f xs = [f x | x <- xs]
  45.  
  46. zipWithMy :: (a -> b -> c) -> [a] -> [b] -> [c]
  47. zipWithMy f (a:as) (b:bs) = f a b : zipWithMy f as bs
  48. zipWithMy _ _ _             = []
  49.  
  50. sumL :: [Int] -> Int
  51. sumL []       = 0
  52. sumL (x:xs) = x + sum xs
  53.  
  54. productMy :: [Int] -> Int
  55. productMy []       = 1
  56. productMy (x:xs) = x * productMy xs
  57.  
  58. andMy :: [Bool] -> Bool
  59. andMy []       = True
  60. andMy (x:xs) = x &&  (andMy xs)
  61.  
  62. foldrMy :: (a -> b -> b) -> b -> [a] -> b
  63. foldrMy f z [] = z
  64. foldrMy f z (x:xs) = f x (foldrMy f z xs)
  65.  
  66. repeated :: (a -> a) -> Int -> (a -> a)
  67. repeated f 1 = f
  68. repeated f n = \x -> f  (repeated f (n - 1) x)
  69.  
  70. derive :: (Float -> Float) -> Float -> (Float -> Float)
  71. derive f dx = \x -> (f (x + dx) - f x) / dx
  72.  
  73. insert :: Int -> [Int] -> [Int]
  74. insert x [] = [x]
  75. insert x (y:ys)
  76.   | x <= y    = x:(y:ys)
  77.   | otherwise = y : (insert x ys)
  78.  
  79.  
  80. insertSort :: [Int] -> [Int]
  81. insertSort [] = []
  82. insertSort (x:xs) = insert x (insertSort xs)
  83.  
  84. qSort :: [Int] -> [Int]
  85. qSort []       = []
  86. qSort (x:xs) = qSort more ++ [x] ++ qSort less
  87.  where
  88.  less = filter (<x) xs
  89.  more = filter (>=x) xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement