-- 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 colorOf colorOf :: Eq a => a -> [Binding a] -> Maybe Color Main> colorOf 7 [CB 7 Red] Just Red Main> colorOf "Brad" [CB "foo" Purple, CB "Brad" Green] Just Green Main> colorOf "bar" [CB "foo" Purple, CB "Brad" Green] Nothing
Main> :type nodesClash nodesClash :: Eq a => [Binding a] -> Edge a -> Bool Main> nodesClash [] (E "ny" "nj") False Main> nodesClash [CB "ny" Red, CB "nj" Green] (E "ny" "nj") False Main> nodesClash [CB "ny" Red, CB "nj" Red] (E "ny" "nj") True Main> nodesClash [CB "ny" Red] (E "ny" "nj") False
Main> :type uniqueNodes uniqueNodes :: Eq a => Graph a -> [a] Main> uniqueNodes [E 1 2] [1,2] Main> uniqueNodes [E 1 1, E 2 2] [1,2] Main> uniqueNodes [E 1 2, E 1 2] [1,2] Main> uniqueNodes [E 1 2, E 2 3, E 1 3] [1,2,3] Main> uniqueNodes [E "NW" "NE", E "NE" "SE", E "SE" "SW", E "SW" "NW"] ["NW","NE","SE","SW"]
Main> :type noClashes noClashes :: Eq a => [Binding a] -> Graph a -> Bool Main> noClashes [] [E 1 2] True Main> noClashes [CB 1 Red, CB 2 Red] [E 1 2] False Main> noClashes [CB 1 Red, CB 2 Green, CB 3 Blue] [E 1 2, E 2 3, E 1 3] True Main> noClashes [CB 1 Red, CB 2 Green, CB 3 Red] [E 1 2, E 2 3, E 1 3] False Main> noClashes [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 assignColors assignColors :: Eq a => [a] -> Graph a -> [Binding a] Main> assignColors ["ny", "nj"] [E "ny" "nj"] [CB "ny" Green,CB "nj" Red] Main> assignColors [1,2,3] [E 1 2, E 2 3, E 1 3] [CB 1 Blue,CB 2 Green,CB 3 Red] Main> assignColors [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> assignColors [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 colorGraph colorGraph :: Eq a => Graph a -> [Binding a] Main> colorGraph [E "ny" "nj"] [CB "ny" Green,CB "nj" Red] Main> colorGraph [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> colorGraph [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]