Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Maybe
- import Test.QuickCheck
- --1
- q2 :: Eq a => [a] -> [a]
- q2 [] = []
- q2 (x:xs) | x `elem` xs = q2 [y | y <- xs, y /= x]
- | otherwise = x: q2 xs
- --2
- rep :: Int -> String -> IO()
- rep n s
- | n <= 0 = return ()
- | otherwise = do putStrLn s
- rep (n - 1) s
- prop_LookJust x table = case (lookup x table) of
- Nothing -> True
- Just y -> (x,y) `elem` table
- splitComma :: String -> (String, String)
- splitComma s = (f,sn)
- where f = takeWhile (/=',') s
- sn = dropWhile (==',') $ dropWhile (/= ',') s
- parseDB :: String -> [(String,String)]
- parseDB s = map splitComma (lines s)
- --3
- data Form
- = And Form Form
- | Or Form Form
- | Not Form
- | Val Bool
- eval :: Form -> Bool
- eval (And f1 f2) = eval f1 && eval f2
- eval (Or f1 f2) = eval f1 || eval f2
- eval (Not f1) = not $ eval f1
- eval (Val b) = b
- --4
- prop_Concat as bs = isPrefixOf as $ as ++ bs
- --5
- lookupDB :: FilePath -> String -> IO String
- lookupDB fp s = do
- s1 <- readFile fp
- return $ fromJust $ lookup s $ parseDB s1
- --7 Mock Exam 2017
- luckyList2 :: Gen [Integer]
- luckyList2 = do n <- arbitrary
- if elem 7 n
- then return n
- else luckyList2
- -- 4 Mock Exam
- data Expr = Num Double | BinOp Op Expr Expr
- deriving (Eq,Show)
- data Op = Add | Mul
- deriving (Eq,Show)
- example = BinOp Add (Num 1)
- (BinOp Add (Num 2)
- (BinOp Mul (Num 3)
- (Num 4)
- )
- )
- countOp :: Op -> Expr -> Int
- countOp op (BinOp op' e1 e2) | op == op' = 1 + countOp op e1 + countOp op e2
- | otherwise = countOp op e1 + countOp op e2
- countOp _ _ = 0
- -- 1 Exam Jan 3 2015 (skulle löst om man kunde filter)
- occurs :: Eq a => a -> [a] -> Int
- occurs x xs = length $ filter (==x) xs
- --2
- --redan löst, anteckningar
- --3
- data Road
- = City String | Fork Road Road
- deriving (Eq,Show)
- middleOfNowhere :: Road
- middleOfNowhere =
- Fork
- ( City "Mora" )
- ( Fork
- ( Fork
- ( City "Kiruna" )
- ( City "Gävle" )
- )
- ( City "Stockholm" )
- )
- {-löste nästan ;(-}
- reachable :: String -> Road -> Bool
- reachable city (City c) = city == c
- reachable city (Fork l r) = reachable city l || reachable city r
- --4
- prop_nub xs = length (nub xs) <= length xs
- --5
- copyFile :: FilePath -> FilePath -> IO ()
- copyFile from to = do
- s <- readFile from
- writeFile to s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement