Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import Data.Maybe
  3. import Test.QuickCheck
  4.  
  5. --1
  6.  
  7. q2 :: Eq a => [a] -> [a]
  8. q2 []               = []
  9. q2 (x:xs) | x `elem` xs = q2 [y | y <- xs, y /= x]
  10.           | otherwise   = x: q2 xs
  11.  
  12.  
  13. --2
  14.  
  15. rep :: Int -> String -> IO()
  16. rep n s
  17.    | n <= 0    = return ()
  18.    | otherwise = do putStrLn s
  19.                     rep (n - 1) s
  20.  
  21. prop_LookJust x table = case (lookup x table) of
  22.         Nothing -> True
  23.         Just y -> (x,y) `elem` table
  24.  
  25.  
  26. splitComma :: String -> (String, String)
  27. splitComma s = (f,sn)
  28.     where f  = takeWhile (/=',') s
  29.           sn = dropWhile (==',') $ dropWhile (/= ',') s
  30.  
  31. parseDB :: String -> [(String,String)]
  32. parseDB s =  map splitComma (lines s)
  33.  
  34.  
  35.  
  36. --3
  37. data Form
  38.   = And Form Form
  39.   | Or Form Form
  40.   | Not Form
  41.   | Val Bool
  42.  
  43. eval :: Form -> Bool
  44. eval (And f1 f2) = eval f1 && eval f2
  45. eval (Or f1 f2)  = eval f1 || eval f2
  46. eval (Not f1)    = not $ eval f1
  47. eval (Val b)    = b
  48.  
  49.  
  50.  --4
  51. prop_Concat as bs = isPrefixOf as $ as ++ bs
  52.    
  53.  
  54. --5
  55.  
  56. lookupDB :: FilePath -> String -> IO String
  57. lookupDB fp s = do
  58.     s1 <- readFile fp
  59.     return $ fromJust $ lookup s $ parseDB s1
  60.  
  61.  
  62.  
  63. --7 Mock Exam 2017
  64. luckyList2 :: Gen [Integer]
  65. luckyList2 = do n <- arbitrary
  66.                 if elem 7 n
  67.                 then return n
  68.                 else luckyList2
  69.  
  70.  
  71. -- 4 Mock Exam
  72.  
  73. data Expr = Num Double | BinOp Op Expr Expr
  74.  deriving (Eq,Show)
  75.  
  76. data Op = Add | Mul
  77.   deriving (Eq,Show)
  78.  
  79.  
  80. example = BinOp Add (Num 1)
  81.                     (BinOp Add (Num 2)
  82.                                (BinOp Mul (Num 3)
  83.                                           (Num 4)
  84.                                )
  85.                     )
  86.  
  87. countOp :: Op -> Expr -> Int
  88. countOp op (BinOp op' e1 e2) | op == op' = 1 + countOp op e1 + countOp op e2
  89.                              | otherwise = countOp op  e1 + countOp op e2
  90. countOp _ _                    = 0
  91.      
  92.  
  93.  
  94.  
  95. -- 1 Exam Jan 3 2015 (skulle löst om man kunde filter)
  96.  
  97. occurs :: Eq a => a -> [a] -> Int
  98. occurs x xs = length $ filter (==x) xs
  99.  
  100. --2
  101. --redan löst, anteckningar
  102.  
  103. --3
  104.  
  105. data Road
  106.  = City String | Fork Road Road
  107.     deriving (Eq,Show)
  108.  
  109.  
  110. middleOfNowhere :: Road
  111. middleOfNowhere =
  112.     Fork
  113.       ( City "Mora" )
  114.       ( Fork
  115.           ( Fork
  116.               ( City "Kiruna" )
  117.               ( City "Gävle" )
  118.           )
  119.           ( City "Stockholm" )
  120.       )
  121.  
  122. {-löste nästan ;(-}
  123. reachable :: String -> Road -> Bool
  124. reachable city (City c) = city == c
  125. reachable city (Fork l r) = reachable city l || reachable city r
  126.  
  127. --4
  128. prop_nub xs = length (nub xs) <= length xs
  129.  
  130.  
  131. --5
  132.  
  133. copyFile :: FilePath -> FilePath -> IO ()
  134. copyFile from to = do
  135.     s <- readFile from
  136.     writeFile to s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement