Advertisement
Zorikto

lab3hask

Jun 4th, 2021
1,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Lab3 where
  2.  
  3. import Data.Char
  4. import Data.Maybe
  5.  
  6. data Key = Shift Char | CapsLock | Sym Char deriving (Eq, Show)
  7.  
  8. a = [Shift 'h', Sym 'e', CapsLock, Sym 'l', Sym 'l', Shift 'o', CapsLock]
  9.  
  10. getAlNum :: [Key] -> [Key]
  11. getAlNum [] = []
  12. getAlNum ((Shift x) : xs) = getAlNum xs
  13. getAlNum ((CapsLock) : xs) = getAlNum xs
  14. getAlNum ((Sym x) : xs) = (Sym x) : getAlNum xs
  15.  
  16. getRaw :: [Key] -> String
  17. getRaw [] = ""
  18. getRaw ((Sym x) : xs) =  x : getRaw xs
  19. getRaw ((Shift x) : xs) = x : getRaw xs
  20. getRaw ((CapsLock) : xs) = getRaw xs
  21.  
  22. isCapsLocked' :: [Key] -> Bool -> Bool
  23. isCapsLocked' [] c1 = c1
  24. isCapsLocked' (x:xs) c1 = if (x == CapsLock) then isCapsLocked' xs (not c1) else isCapsLocked' xs c1
  25. isCapsLocked :: [Key] -> Bool
  26. isCapsLocked [] = False
  27. isCapsLocked (x:xs) = isCapsLocked' (x:xs) False
  28.  
  29.  
  30. getSym :: Key -> Char
  31. getSym (Sym   x) = x
  32. getSym (Shift x) = x
  33. typCode :: Key -> Int
  34. typCode (Sym _)   = 0
  35. typCode CapsLock  = -1
  36. typCode (Shift _) = 1
  37. getString' :: [Key] -> Bool -> String
  38. getString' [] _ = ""
  39. getString' (x:xs) cl | ((typCode x) == -1) = getString' xs (not cl)
  40.                     | ((typCode x) == 0) =  if cl then (toUpper (getSym x)) : (getString' xs cl) else (getSym x) : (getString' xs cl)
  41.                     | otherwise = if (not cl) then (toUpper(getSym x)) : (getString' xs cl) else (getSym x) : (getString' xs cl)
  42. getString :: [Key] -> String
  43. getString [] = []
  44. getString (x:xs) = getString' (x:xs) False
  45.  
  46.  
  47. --10
  48. data Zadanie = Lab String Int
  49.               | Rgz String
  50.               | Ref String String
  51.               deriving (Eq, Show)
  52.  
  53. type Plan = [(Zadanie, Maybe Int)]
  54.  
  55. plan = [(Lab "math" 1, Just 1),(Lab "math" 2, Just 2),(Lab "math" 3, Nothing),
  56.        (Lab "math" 4, Nothing),(Rgz "phys", Just 2),(Ref "hist" "world war 2", Just 1),(Ref "hist" "world war 3", Nothing)] :: Plan
  57.  
  58. predmet :: Zadanie -> String
  59. predmet (Lab x _) = x
  60. predmet (Rgz x) = x
  61. predmet (Ref x _) = x
  62.  
  63. getByTitle :: String -> Plan -> [Zadanie]
  64. getByTitle _ [] = []
  65. getByTitle "" _ = []
  66. getByTitle title (x:xs) = if title == predmet (fst x) && Nothing == snd x
  67.                            then fst x : getByTitle title xs
  68.                          else getByTitle title xs
  69.  
  70. getReferats :: Plan -> [String]
  71. getReferats [] = []
  72. getReferats ((Lab _ _, _):xs) = getReferats xs
  73. getReferats ((Rgz _, _):xs) = getReferats xs
  74. getReferats ((Ref _ t, _):xs) = t : getReferats xs
  75.  
  76. getRest :: Plan -> [Zadanie]
  77. getRest [] = []
  78. getRest (x:xs) = if Nothing == snd x
  79.                    then fst x : getRest xs
  80.                 else getRest xs
  81.  
  82. getRestForWeek :: Int -> Plan -> [Zadanie]
  83. getRestForWeek _ [] = []
  84. getRestForWeek n (x:xs) = if Just n >= snd x && snd x /= Nothing then fst x : getRestForWeek n xs else getRestForWeek n xs
  85.  
  86.  
  87. getCount :: Int -> Maybe Int -> Plan -> Int
  88. getCount n _ [] = n
  89. getCount n a (x:xs) = if snd x == a
  90.                          then getCount (n+1) a xs
  91.                      else getCount n a xs;
  92.  
  93. getNums :: Plan -> [Maybe Int]
  94. getNums [] = []
  95. getNums (x:xs) = if snd x /= Nothing then (snd x) : getNums xs else getNums xs
  96.  
  97. getUniNums :: [Maybe Int] -> [Maybe Int]
  98. getUniNums [] = []
  99. getUniNums (x:xs) = if x `elem` xs then getUniNums xs else x : getUniNums xs
  100.  
  101. getPlot' :: [Maybe Int] -> [(Maybe Int,Int)]
  102. getPlot' [] = []
  103. getPlot' (x:xs) = if x /= Nothing
  104.                         then(x, getCount 0 x plan) : getPlot' xs
  105.                  else getPlot' xs
  106. getPlot :: Plan->[(Maybe Int,Int)]
  107. getPlot [] = []
  108. getPlot (x:xs) = getPlot' (getUniNums (getNums (x:xs)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement