-- Here's an enumerated type for representing colors. In the rest of the
-- code you write, these constructors should NOT be used explicitly. One
-- should be able to add or remove Color values in the type definition
-- without having to modify ANY other code.
data Color = Red | Green | Blue | Yellow | Purple | Brown
deriving (Eq, Enum, Show, Bounded)
-- We also need a structure for associating a color with an item. Note
-- that this type is parameterized, and can associate colors with items
-- of ANY type.
data Binding a = CB a Color
deriving (Eq, Show)
Main> :type color_of color_of :: Eq a => a -> [Binding a] -> Maybe Color Main> color_of 7 [CB 7 Red] Just Red Main> color_of "Brad" [CB "foo" Purple, CB "Brad" Green] Just Green Main> color_of "bar" [CB "foo" Purple, CB "Brad" Green] Nothing
Main> :type nodes_clash nodes_clash :: Eq a => [Binding a] -> Edge a -> Bool Main> nodes_clash [] (E "ny" "nj") False Main> nodes_clash [CB "ny" Red, CB "nj" Green] (E "ny" "nj") False Main> nodes_clash [CB "ny" Red, CB "nj" Red] (E "ny" "nj") True Main> nodes_clash [CB "ny" Red] (E "ny" "nj") False
Main> :type unique_nodes unique_nodes :: Eq a => Graph a -> [a] Main> unique_nodes [E 1 2] [1,2] Main> unique_nodes [E 1 1, E 2 2] [1,2] Main> unique_nodes [E 1 2, E 1 2] [1,2] Main> unique_nodes [E 1 2, E 2 3, E 1 3] [1,2,3] Main> unique_nodes [E "NW" "NE", E "NE" "SE", E "SE" "SW", E "SW" "NW"] ["NW","NE","SE","SW"]
Main> :type no_clashes no_clashes :: Eq a => [Binding a] -> Graph a -> Bool Main> no_clashes [] [E 1 2] True Main> no_clashes [CB 1 Red, CB 2 Red] [E 1 2] False Main> no_clashes [CB 1 Red, CB 2 Green, CB 3 Blue] [E 1 2, E 2 3, E 1 3] True Main> no_clashes [CB 1 Red, CB 2 Green, CB 3 Red] [E 1 2, E 2 3, E 1 3] False Main> no_clashes [CB 1 Red, CB 2 Green, CB 3 Red, CB 4 Green] [E 1 2, E 2 3, E 3 4, E 4 1] True
Main> :type assign_colors assign_colors :: Eq a => [a] -> Graph a -> [Binding a] Main> assign_colors ["ny", "nj"] [E "ny" "nj"] [CB "ny" Green,CB "nj" Red] Main> assign_colors [1,2,3] [E 1 2, E 2 3, E 1 3] [CB 1 Blue,CB 2 Green,CB 3 Red] Main> assign_colors [1,2,3,4] [E 1 2, E 2 3, E 3 4, E 4 1] [CB 1 Green,CB 2 Red,CB 3 Green,CB 4 Red] Main> assign_colors [1,2,3,4] [E 1 2, E 2 3, E 3 4, E 4 1, E 1 3] [CB 1 Blue,CB 2 Red,CB 3 Green,CB 4 Red]
Main> :type color_graph color_graph :: Eq a => Graph a -> [Binding a] Main> color_graph [E "ny" "nj"] [CB "ny" Green,CB "nj" Red] Main> color_graph [E 1 2, E 2 3, E 3 4, E 4 1] [CB 1 Green,CB 2 Red,CB 3 Green,CB 4 Red] Main> color_graph [E 1 2, E 2 3, E 3 4, E 4 1, E 1 3] [CB 1 Blue,CB 2 Red,CB 3 Green,CB 4 Red]