-- A datatype for lists data List a = Empty | Add a (List a) deriving (Eq, Show) -- type Hand = List Card -- tom l checks whether l is empty tom :: List a -> Bool tom Empty = True tom _ = False -- forst list returns the first element of the list forst :: List a -> a forst (Add apa _) = apa forst Empty = error "forst Empty" forst' :: List a -> Maybe a forst' (Add a _) = Just a forst' Empty = Nothing -- sist l returns the last element of l sist :: List a -> a sist (Add a Empty) = a sist (Add _ list) = sist list sist Empty = error "sist Empty" rev :: List a -> List a rev Empty = Empty rev (Add a list) = rev list +++ Add a Empty (+++) :: List a -> List a -> List a Empty +++ l2 = l2 Add a l1 +++ l2 = Add a (l1 +++ l2) append :: List a -> List a -> List a append Empty l2 = l2 append (Add a l1) l2 = Add a (append l1 l2) list1 = Add 1 (Add 2 Empty) list2 = Add 3 (Add 4 Empty) -- alternative sist' :: List a -> a sist' list = forst (rev list) -- Introduce standard lists forstL :: [a] -> a forstL (a:_) = a forstL [] = error "forstL []" -- l1 ++++ l2 (a.k.a. ++) appends l2 after l1 [] ++++ bs = bs (a:as) ++++ bs = a : (as ++++ bs) -- size l (a.k.a length) returns the number of elements in l size :: [a] -> Int size [] = 0 size (_:as) = 1 + size as -- summan xs (a.k.a sum) calculates the sum of all elements in xs summan :: Num a => [a] -> a summan [] = 0 summan (a:as) = a + summan as list3 = [1,2,3] :: [Int] list4 = [1,2,3] :: [Double] -- mest l (a.k.a. maximum) returns the largest element in l mest :: Ord a => [a] -> a mest [] = error "mest []" mest [a] = a mest (a:as) = max a (mest as)