module Work where -- A datatype for lists -- data ListOfInt = Empty | Add Integer ListOfInt -- data ListOfFloat = Empty' | Add' Float ListOfFloat data List a = Empty | Add a (List a) -- 1,2,3 list1 :: List Integer list1 = Add 1 (Add 2 (Add 3 Empty)) -- 2.3, 3.4 list2 :: List Float list2 = Add 2.3 (Add 3.4 Empty) data FI = F Float | I Integer -- A list with elements of different type list3 :: List FI list3 = Add (F 2.3) (Add (I 4) Empty) pair1 :: (Float, Integer) pair1 = (2.3, 4) -- Slides: type of constructors -- Socrative: type of isNumeric -- Use List to define Hand -- type Hand = List Card -- tom l checks whether l is empty tom :: List t -> Bool tom Empty = True tom _ = False -- Type variables begins with a small letter -- forst list returns the first element of the list forst :: List t -> t forst Empty = error "forst: empty list" forst (Add a _) = a -- sist l returns the last element of l sist :: List t -> t sist Empty = error "sist: empty list" sist (Add a Empty) = a sist (Add a as) = sist as -- Slides: standard lists -- Define these functions for the standard lists -- size l (a.k.a length) returns the number of elements in l size' :: List a -> Integer size' Empty = 0 size' (Add a as) = 1 + size' as size :: [a] -> Integer 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 (x:xs) = x + summan xs -- What is the most general type of summan? -- Socrative: definition of mest -- mest l (a.k.a. maximum) returns the largest element in l -- l1 +++ l2 (a.k.a. ++) appends l2 after l1 -- rev (a.k.a. reverse) reverses the elements in l -- value s returns the number represented by the string s as an integer -- Property of value and show