Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- 2.
  2. import CSVUtils
  3. import Data.List
  4. import Data.Monoid
  5.  
  6. type Decision = String
  7. caseA = ["Raining", "Yes", "Workday"]
  8. caseB = ["Sunny", "No", "Workday"]
  9.  
  10. nbDecide :: CSV -> [String] -> Decision
  11. nbDecide abc xs = if prob1 > prob2 then c1 else c2
  12.   where (c1,c2) = (head . nub $ gys 3 csv, (nub $ gys 3 csv) !! 1)
  13.         gys n   = colFields n
  14.         csv     = drop 1 abc
  15.         rc1     = filter ((==c1) . (!! 3)) csv
  16.         rc2     = filter ((==c2) . (!! 3)) csv
  17.         pr ys x = realToFrac  (cnt x ys) / realToFrac (length ys)
  18.         prob1   = (pr (gys 3 csv) c1) * (pr (gys 0 rc1) (xs !! 0)) * (pr (gys 1 rc1) (xs !! 1)) * (pr (gys 2 rc1) (xs !! 2))
  19.         prob2   = (pr (gys 3 csv) c2) * (pr (gys 0 rc2) (xs !! 0)) * (pr (gys 1 rc2) (xs !! 1)) * (pr (gys 2 rc2) (xs !! 2))
  20.  
  21. cnt :: String -> [String] -> Int
  22. cnt x = length . filter (==x)
  23.  
  24. nbDecideAll :: CSV -> [[String]] -> [Decision]
  25. nbDecideAll csv = map (nbDecide csv)
  26.  
  27. -- 3.
  28.  
  29. class Truthy a where
  30.   truey :: a -> Bool
  31.   falsey :: a -> Bool
  32.   truey = not . falsey
  33.   falsey = not . truey
  34.  
  35. instance Truthy Int where
  36.   falsey 0 = True
  37.   falsey _ = False
  38.  
  39. instance Truthy Bool where
  40.   truey a = a
  41.  
  42. instance Truthy [a] where
  43.   truey [] = False
  44.   truey _  = True
  45.  
  46. if' :: Truthy p => p -> a -> a -> a
  47. if' tr thn els = if truey tr then thn else els
  48.  
  49. assert  :: Truthy p => p -> a -> a
  50. assert x y = if' x y $ error "Assertion failed"
  51.  
  52. (&&&) :: (Truthy a, Truthy b) => a -> b -> Bool
  53. a &&& b = truey a && truey b
  54.  
  55. (|||) :: (Truthy a, Truthy b) => a -> b -> Bool
  56. a ||| b = truey a || truey b
  57.  
  58. -- 4.
  59.  
  60. data DiffList a = DiffList{
  61.  undiff :: [a] -> [a]
  62. }
  63.  
  64. dl = DiffList (\xs -> concat ([[1,2],[3]]) ++ xs)
  65.  
  66. empty :: DiffList a
  67. empty = DiffList id
  68.  
  69. fromList :: [a] -> DiffList a
  70. fromList xs = DiffList (xs++)
  71.  
  72. toList :: DiffList a -> [a]
  73. toList dL = undiff dL []
  74.  
  75. append :: DiffList a -> DiffList a -> DiffList a
  76. append dl1 dl2 = DiffList (undiff dl1 . undiff dl2)
  77.  
  78. instance Monoid (DiffList a) where
  79.  mempty = empty
  80.  mappend = append
  81.  
  82. -- 5.
  83. data MyList a = Cons a (MyList a) | Nil deriving Show
  84.  
  85. instance Functor MyList where
  86.  fmap f (Cons a next) = Cons (f a) (fmap f next)
  87.  fmap _ Nil           = Nil
  88.  
  89. instance Applicative MyList where
  90.  pure a = Cons a Nil
  91.  Nil <*> _   = Nil
  92.  _   <*> Nil = Nil
  93.  Cons f n <*> list = conc (fmap f list) (n <*> list)
  94.  
  95. conc :: MyList a -> MyList a -> MyList a
  96. conc Nil list = list
  97. conc (Cons a b) list = Cons a (conc b list)
  98.  
  99. instance Monad MyList where
  100.  l >>= f = connect $ fmap f l
  101.  
  102. connect :: MyList (MyList a) -> MyList a
  103. connect Nil = Nil
  104. connect (Cons xs xss) = conc xs $ connect xss
  105.  
  106. fun :: Int -> MyList Int
  107. fun x = Cons (x + 1) (Cons (x * 2) Nil)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement