Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 2.
- import CSVUtils
- import Data.List
- import Data.Monoid
- type Decision = String
- caseA = ["Raining", "Yes", "Workday"]
- caseB = ["Sunny", "No", "Workday"]
- nbDecide :: CSV -> [String] -> Decision
- nbDecide abc xs = if prob1 > prob2 then c1 else c2
- where (c1,c2) = (head . nub $ gys 3 csv, (nub $ gys 3 csv) !! 1)
- gys n = colFields n
- csv = drop 1 abc
- rc1 = filter ((==c1) . (!! 3)) csv
- rc2 = filter ((==c2) . (!! 3)) csv
- pr ys x = realToFrac (cnt x ys) / realToFrac (length ys)
- prob1 = (pr (gys 3 csv) c1) * (pr (gys 0 rc1) (xs !! 0)) * (pr (gys 1 rc1) (xs !! 1)) * (pr (gys 2 rc1) (xs !! 2))
- prob2 = (pr (gys 3 csv) c2) * (pr (gys 0 rc2) (xs !! 0)) * (pr (gys 1 rc2) (xs !! 1)) * (pr (gys 2 rc2) (xs !! 2))
- cnt :: String -> [String] -> Int
- cnt x = length . filter (==x)
- nbDecideAll :: CSV -> [[String]] -> [Decision]
- nbDecideAll csv = map (nbDecide csv)
- -- 3.
- class Truthy a where
- truey :: a -> Bool
- falsey :: a -> Bool
- truey = not . falsey
- falsey = not . truey
- instance Truthy Int where
- falsey 0 = True
- falsey _ = False
- instance Truthy Bool where
- truey a = a
- instance Truthy [a] where
- truey [] = False
- truey _ = True
- if' :: Truthy p => p -> a -> a -> a
- if' tr thn els = if truey tr then thn else els
- assert :: Truthy p => p -> a -> a
- assert x y = if' x y $ error "Assertion failed"
- (&&&) :: (Truthy a, Truthy b) => a -> b -> Bool
- a &&& b = truey a && truey b
- (|||) :: (Truthy a, Truthy b) => a -> b -> Bool
- a ||| b = truey a || truey b
- -- 4.
- data DiffList a = DiffList{
- undiff :: [a] -> [a]
- }
- dl = DiffList (\xs -> concat ([[1,2],[3]]) ++ xs)
- empty :: DiffList a
- empty = DiffList id
- fromList :: [a] -> DiffList a
- fromList xs = DiffList (xs++)
- toList :: DiffList a -> [a]
- toList dL = undiff dL []
- append :: DiffList a -> DiffList a -> DiffList a
- append dl1 dl2 = DiffList (undiff dl1 . undiff dl2)
- instance Monoid (DiffList a) where
- mempty = empty
- mappend = append
- -- 5.
- data MyList a = Cons a (MyList a) | Nil deriving Show
- instance Functor MyList where
- fmap f (Cons a next) = Cons (f a) (fmap f next)
- fmap _ Nil = Nil
- instance Applicative MyList where
- pure a = Cons a Nil
- Nil <*> _ = Nil
- _ <*> Nil = Nil
- Cons f n <*> list = conc (fmap f list) (n <*> list)
- conc :: MyList a -> MyList a -> MyList a
- conc Nil list = list
- conc (Cons a b) list = Cons a (conc b list)
- instance Monad MyList where
- l >>= f = connect $ fmap f l
- connect :: MyList (MyList a) -> MyList a
- connect Nil = Nil
- connect (Cons xs xss) = conc xs $ connect xss
- fun :: Int -> MyList Int
- fun x = Cons (x + 1) (Cons (x * 2) Nil)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement