Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 5. Algebraic Datatypes in Haskell ====== ===== The type List ===== 1. Define the type //list with elements of type ''Integer''//: <code haskell> data IntList = ... </code> 2. Define a function which computes the sum of elements of such a list: <code haskell> isum :: IntList -> Integer </code> 3. Define type **polymorfic** type ''List a'' encoding lists with elements of type ''a'': <code haskell> data List a = </code> 4. Define a function which converts ''IntList'' lists to ''List Integer'' lists: <code haskell> to_poly_list :: IntList -> List Integer </code> 5. Define a function which displays lists. What type will this function have? <code haskell> show_list = ... </code> Add the tree datatype definition from the lecture: <code haskell> data Tree a = Void | Node (Tree a) a (Tree a) deriving Show </code> 6. Implement the function ''flatten'': <code haskell> flatten :: Tree a -> List a </code> 7. Define list concatenation over type ''List a'': <code haskell> app :: (List a) -> (List a) -> (List a) </code> 8. Define the function ''tmap'' which is the ''Tree a'' correspondent to ''map::(a->b) -> [a] -> [b]''. 9. Define the function ''tzipWith'': <code haskell> tzipWith :: (a -> b -> c) -> (Tree a) -> (Tree b) -> (Tree c) </code> 10. Define the function ''tfoldr'': <code haskell> tfoldr :: (a -> b -> b) -> b -> Tree a -> b </code> Before implementing it, consider what should ''tfoldr (+) 0'' do, and how should this value be computed. 11. Implement the flattening function using ''tfoldr'': <code haskell> tflatten = ... </code> Sometimes, instead of doing pattern matching in a function definition, e.g.: <code haskell> f [] = "Empty" f (x:y:xs) = "At least two" f _ = "Something else" </code> it is useful to do this in the body of the function, or in another expression. This can be done using the ''case'' instruction. Example: <code haskell> f l = case l of [] -> "Empty" (x:y:xs) -> "At least two" _ -> "Something else" </code> 12. Consider the following definition of natural numbers extended with the value ''Infinity'': <code haskell> data Extended = Infinity | Value Integer </code> Define a function which computes the sum of two ''Extended'' values: <code haskell> extSum :: Extended -> Extended -> Extended extSum v v' = </code> (Question: can you implement it with a single ''case'' expression?) 13. Define a function which computes the equality of two ''Extended'' values: <code haskell> equal :: Extended -> Extended -> Bool </code> The polymorphic datatype: <code haskell> data Maybe a = Nothing | Just a </code> which is part of the Haskell default library is useful for error handling. For instance, if a computation fails, a function may return ''Nothing''. If the computation returns a result ''x'' of type ''a'', then the function will return a value ''Just x''. 14. Implement the function ''lhead'' which behaves like ''head'' but returns ''Nothing'' if the argument of the function is the empty list: <code haskell> lhead :: List a -> Maybe a </code> 15. Implement the function ''ltail''