Advertisement
GerexD

hsk2

Jan 2nd, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. -- Gere Andor
  2. -- gaim2019
  3. -- 522/1
  4.  
  5. -- Funk2
  6.  
  7. import Data.Char
  8. --import Data.List.Split
  9.  
  10. --------------------------------- 1.
  11.  
  12. torol_szokoz :: String -> String
  13. torol_szokoz [] = []
  14. torol_szokoz (' ':xs) = torol_szokoz(xs)
  15. torol_szokoz (x:xs) = toLower x:torol_szokoz(xs)
  16.  
  17. string_buffer :: Int -> String
  18. string_buffer 0 = []
  19. string_buffer n = " " ++ string_buffer (n-1)
  20.  
  21. string_negyszog :: Int -> String -> [String]
  22. string_negyszog n s
  23. | length s>=n = take n s : string_negyszog n (drop n s)
  24. | length s>0 = [s ++ string_buffer (n-(length s))]
  25. | otherwise = []
  26.  
  27. string_oszlop :: [String] -> String
  28. string_oszlop [] = []
  29. string_oszlop [[]] = []
  30. string_oszlop [x:xs] = [x] ++ string_oszlop [xs]
  31.  
  32. --------------------------------- 2.
  33.  
  34. oldPhone_lehet :: [Char] -> Bool
  35. oldPhone_lehet [] = True
  36. oldPhone_lehet (x:xs)
  37. | last (oldPhone [x]) == ('-',0) = False
  38. | otherwise = oldPhone_lehet xs
  39.  
  40. oldPhone :: Num a => [Char] -> [(Char, a)]
  41. oldPhone [] = []
  42. oldPhone (x:xs)
  43. | isUpper x = ('*',1):(keres gombok (toLower x)):(oldPhone xs)
  44. | otherwise = (gomb,n):(oldPhone xs)
  45. where gombok=["1","aábc2","deéf3","ghií4","jkl5","mnoóöő6","pqrs7","tuúüűv8","wxyz9","+ 0",".,#","*"]
  46. (gomb,n) = keres gombok x
  47.  
  48. keres :: Num a => [[Char]] -> Char -> (Char, a)
  49. keres [] _ = ('-',0)
  50. keres (x:xs) s
  51. | (keres2 x s 1) == ('0',0) = keres xs s
  52. | otherwise = (keres2 x s 1)
  53.  
  54. keres2 :: Num t => [Char] -> Char -> t -> (Char, t)
  55. keres2 [] _ _ = ('0',0)
  56. keres2 (x:xs) s i
  57. | x == s && length xs == 0 = (x,i)
  58. | x == s = ((last xs),i)
  59. | otherwise = keres2 xs s (i+1)
  60. keres2 [x] _ i = (x,i)
  61.  
  62. --------------------------------- 3.
  63.  
  64. valos_preciz :: (Double, Integer)
  65. valos_preciz = until feltetel iteral (2,1)
  66. where
  67. feltetel (x, hatvany) = x/2 == 0
  68. iteral (x, hatvany) = (x*(1/2), hatvany+1)
  69.  
  70. --------------------------------- 4.
  71.  
  72. ln :: (Ord a, Floating a) => a -> a
  73. ln x
  74. | abs x >=1 = -ln2 (1/x)
  75. |otherwise = ln2 (1-x)
  76. where y = x-1
  77.  
  78. ln2 :: (Floating a, Ord a) => a -> a
  79. ln2 y = -z
  80. where
  81. (z,_,_) = until felt iter (0,1,1)
  82. felt (z1,z2,_) = (abs (z1-z2)) < 1e-12
  83. iter (z1,z2,i) = (z1+((-l)**i/i),z1,i+1) where l = y-1
  84.  
  85. --------------------------------- 5.
  86.  
  87. cumul_op :: (a -> a -> a) -> [a] -> [a]
  88. cumul_op2 :: (a -> a -> a) -> [a] -> [a]
  89. cumul_op sz (x:xs) = x:cumul_op2 sz (x:xs)
  90.  
  91. cumul_op2 sz (x:[]) = []
  92. cumul_op2 sz (x1:x2:xs) = x:cumul_op2 sz (x:xs) where x=(sz x1 x2)
  93.  
  94. --------------------------------- 6.
  95.  
  96. data BinFa a =
  97. Nodus (BinFa a) a (BinFa a)
  98. | Level
  99. deriving Show
  100.  
  101. beszur :: Ord a => BinFa a -> a -> BinFa a
  102. beszur Level x = Nodus Level x Level
  103. beszur (Nodus bal a jobb) x
  104. | a == x = Nodus bal a jobb
  105. | a < x = Nodus bal a (beszur jobb x)
  106. | a > x = Nodus (beszur bal x) a jobb
  107.  
  108. listabol :: Ord a => [a] -> BinFa a
  109. listabol = foldl beszur Level
  110.  
  111. torol :: Ord a => BinFa a -> a -> Maybe (BinFa a)
  112. torol Level x = Nothing
  113. torol (Nodus bal a jobb) x = case compare x a of
  114. GT -> case torol jobb x of
  115. Nothing -> Nothing
  116. Just binfa -> Just (Nodus bal a binfa)
  117. LT -> case torol bal x of
  118. Nothing -> Nothing
  119. Just binfa -> Just (Nodus binfa a jobb)
  120. EQ -> torol2 (Nodus bal a jobb) x
  121.  
  122. torol2 :: Ord a => BinFa a -> a -> Maybe (BinFa a)
  123. torol2 (Nodus Level a Level) x = Just Level
  124. torol2 (Nodus Level a j) _ = Just j
  125. torol2 (Nodus b a Level) _ = Just b
  126. torol2 (Nodus b a j) _ = Just (Nodus b uj jj)
  127. where uj = mini j
  128. Just jj = torol j uj
  129. mini (Nodus Level uj _) = uj
  130. mini (Nodus b _ _) = mini b
  131.  
  132. --------------------------------- 7.
  133.  
  134. data COMPLEX = Comp {a :: Float, b :: Float}
  135.  
  136. instance Show COMPLEX where
  137. show (Comp a b)
  138. | b==0 = show a
  139. | b<0 = show a ++ show b ++ "i"
  140. | otherwise = show a ++ "+" ++ show b ++ "i"
  141.  
  142. instance Num COMPLEX where
  143. (Comp a b) + (Comp c d) = (Comp (a+c) (b+d))
  144. (Comp a b) - (Comp c d) = (Comp (a-c) (b-d))
  145. (Comp a b) * (Comp c d) = (Comp (a*c - b*d) (b*d + a*d))
  146. abs (Comp a b) = Comp (sqrt (a*a + b*b)) 0
  147.  
  148. instance Fractional COMPLEX where
  149. (Comp a b) / (Comp c d) = (Comp ((a*c + b*d) / (c*c + d*d)) ((b*c - a*d) / (c*c + d*d)))
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement