{- Starter program for Assignment 8, CSE 341, Winter 2003 simplifying set expressions in Haskell This is the same as starter.hs, except that it uses a fancier test function. (The test function is more versatile, but uses concepts we won't cover in class, specifically I/O monads.) -} union :: Eq a => [a] -> [a] -> [a] -- write your union function here -- the type says that it takes a list of a's, which must be a type -- that supports equality tests -- -- the following definition of union is wrong -- it's just to get Haskell -- to accept this program union x y = [] -- also write declarations and definitions for intersect and difference -- here is a partial definition of the SetExpr data type -- you just need to add elements for Intersect and Difference data SetExpr = Set [Integer] | Var String | Union SetExpr SetExpr deriving (Eq,Show,Read) -- here is the type declaration for set_simplify, and an (incorrect) -- definition (all this version does is simplify an expression to itself) set_simplify :: SetExpr -> SetExpr set_simplify s = s {- tests is a list of SetExpr to use in testing set_simplify. It should consist of pairs of expressions: an original, and the expected result of the simplification. you should add additional tests to the list -} tests :: [SetExpr] tests = [ Set [1,3], Set [1,3], (Union (Var "x") (Var "x")), (Var "x"), (Union (Var "x") (Var "y")), (Union (Var "x") (Var "y")) ] {- runtests is a function to run all the tests in a list of pairs of SetExpr. runtests uses monads, which allows easy input/output. (We will probably not get to monads in 341; in that case, just use this code without worrying about how it works.) -} runtests [] = do return (True) runtests ( orig : expected : tests ) = do putStr "Original: " putStrLn (show orig) putStr "Expected result: " putStrLn (show expected) let actual = set_simplify orig putStr "Actual result: " putStrLn (show actual) let passed = (expected==actual) putStr "Passed? " putStrLn (show passed) putStrLn "" otherresults <- runtests tests return (passed && otherresults) -- function to run all the tests in 'tests'. -- Use this to test your program run = do r <- runtests tests putStr "Passed all tests? " putStrLn (show r)