module Week10 where data List = Empty | Cons Int List deriving (Eq, Show) myShow :: List -> String myShow Empty = "*" myShow (first `Cons` rest) = (show first)++"-"++(myShow rest) myHead :: List -> Int myHead Empty = error "myHead: can't find head of empty list" myHead (Cons first _) = first myTail :: List -> List myTail Empty = error "myTail: can't find tail of empty list" myTail (Cons _ rest) = rest -- unit tests numTests :: Int numTests = 4 passAll :: Bool passAll = pass [1 .. numTests] pass :: [Int] -> Bool pass [] = True pass (firstTest:restTests) = (test firstTest) && (pass restTests) test :: Int -> Bool test 1 = (Cons 4 (Cons 2 emptyList)) == list42 test 2 | (myShow list42) /= "4-2-*" = False | (myShow emptyList) /= "*" = False | otherwise = True test 3 = (myHead list42) == 4 test 4 = (myTail list42) == list2 -- some sample lists emptyList :: List emptyList = Empty list42 :: List list42 = Cons 4 (Cons 2 (Empty)) list2 :: List list2 = Cons 2 (Empty) data Tree = Leaf | Node Tree Tree deriving (Show, Eq)