




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Assigned February 4, 2009 Due February 16, 2009
The purpose of this MP is to give you practice using types in Haskell. You will learn some of the more common types, learn how to write functions that convert from one type to another, and implement several data-structures.
The first type you will use is that for trees.
1 data Tree a = Branch a (Tree a) (Tree a) 2 | Empty
Notice that the type is recursive.
It would be nice to be able to see the type. Haskell provides a type class called Show for types that have a show function defined for them. The first thing we will want to do is tell Haskell that our tree belongs to this type class. The instance line may remind you of the implements keyword in Java. It says that if type variable a is showable, then Tree a is also showable.
1 instance (Show a) ⇒ Show (Tree a) where 2 show Empty = "-" 3 show (Branch a Empty Empty) = show a 4 show (Branch a left right) = "(" ++ show a ++ " " ++ 5 show left ++ " " ++ show right ++ ")"
Okay, truth in advertising: there is a keyword deriving that can make things showable without having to define anything. But it’s better if you understand what’s going on “underneath the hood” before you start using that. So don’t use deriving in this MP. You can use it for future ones.
Example
*Main> Branch 30 Empty (Branch 35 Empty Empty) (30 - 35)
Now that we can see things, you should write add. It will take an element and a tree, and return a new tree. If you try to add something to a tree that already has it, make sure your code replaces the item with the new element. You won’t be able to test that just yet, but it will come in handy later on.
Example
*Main> :t add add :: (Ord a) => a -> Tree a -> Tree a *Main> add 10 Empty 10 *Main> add 20 it (10 - 20) *Main> add 4 it (10 4 20) *Main> add 43 it (10 4 (20 - 43)) *Main> add 3 it (10 (4 3 -) (20 - 43))
The Ord a says that Haskell expects the elements of the tree to be comparable. It determined this from compiling the code; you do not need to tell it the type.
1 add elt Empty = undefined 2 add elt (Branch x left right) 3 | elt < x = undefined 4 | elt > x = undefined 5 | otherwise = undefined
Now write a find function that takes a tree and an element and indicates whether or not the element exists within the tree. This version of find will assume that the tree is a BST. This code will be a lot more readable if you use guards.
Example
*Main> :t find find :: (Ord a) => Tree a -> a -> Bool *Main> let t = foldr add Empty [1,5,3,6,2,4] *Main> t (4 (2 1 3) (6 5 -)) *Main> find t 2
If you forget, you will see an error message like this one:
*Main> map (+ 1) t
Example
*Main> :t Main.map Main.map :: (t -> a) -> Tree t -> Tree a *Main> let t = foldr add Empty [1,5,3,6,2,4] *Main> Main.map (+1) t (5 (3 2 4) (7 6 -))
1 map = undefined
3 Lists
Now you will get to try writing a type. You will make your very own kind of list.
Create a type for lists. Call the type Mylist. It should have two constructors, Cons and Nil.
Example
*Main> :t Cons 30 Nil Cons 30 Nil :: (Num t) => Mylist t *Main> :t Nil Nil :: Mylist a
1 -- data Mylist a = ◦ ..
We want to declare this kind of list to be showable, in the same way that trees were declared to be showable above. We want it to look like built-in lists, but we’ll use curly braces instead of square braces so they will be easy to distinguish.
Example
*Main> Nil {} *Main> Cons 30 Nil {30} *Main> Cons 30 (Cons 40 Nil) {30, 40}
1 instance (Show a) ⇒ Show (Mylist a) where 2 show = undefined
Write a function l2ml that takes a Haskell list and returns an equivalent Mylist.
Example
*Main> :t l2ml l2ml :: [a] -> Mylist a *Main> l2ml [2,4,6,7] {2, 4, 6, 7}
Code
1 l2ml = undefined
Now write the inverse function ml2l.
Example
*Main> :t ml2l ml2l :: Mylist a -> [a] *Main> ml2l (l2ml [2,4,6,7]) [2,4,6,7]
Code
1 -- data ◦ ..
Now make this an instance of Show.
Example
*Main> KV "hi" 2 "hi" --> 2
Code
1 -- instance (Show a, Show b) ⇒ ◦ ..
Now we want to make this type an instance of Eq and Ord so that we can add it to our tree. We only want to compare against the first element. If the keys are equal, then the KV Pairs are equal. To make it an instance of Eq, define the function (==) a b that takes two KV Pairs a and b and indicates if they are equal. To make an instance of Ord, define a function compare that takes two pairs, and returns GT, LT, or EQ as appropriate.
Example
*Main> let a = KV "me" 123 *Main> let b = KV "you" 234 *Main> let c = KV "me" 4543 *Main> a < b True *Main> a < c False *Main> a == c True *Main> a == b False
Code
1 -- instance (Eq a) ⇒ ◦ .. 2 3 -- instance (Ord a) ⇒ ◦ ..
Now we can add these things to our tree.
*Main> let t = add a Empty *Main> t "me" --> 123 *Main> let t2 = add b t *Main> t ("me" --> 123 - "you" --> 234) *Main> let t3 = add c t *Main> t ("me" --> 4543 - "you" --> 234)
Write a function lookup that takes a Tree (KV a b), an a and returns a Maybe b. The type Maybe is built in. It has the following definition:
type Maybe a = Just a | Nothing
Example
*Main> let t = add (KV "jenny" 8675309) (add (KV "emergency" 911) (add (KV "empire" 5882300) Empty)) *Main> tlookup t "jenny" Just 8675309 *Main> tlookup t "helpdesk" Nothing
Code
1 tlookup = undefined
5 Handing In
Put all these functions in a file mp3.hs or mp3.lhs, commit it to your repository, and push it to the server. Try cloning your repository from the server to see if it has everything you expected in it. This MP is due in nine days, so that you have two weekends to finish it. Do not count on staff being able to answer email on the weekend before it is due! Get started earlier!