Skip to content

Instantly share code, notes, and snippets.

@BartG95
Last active February 27, 2016 09:43
Show Gist options
  • Save BartG95/ebf9fcc9e813b125f8a1 to your computer and use it in GitHub Desktop.
Save BartG95/ebf9fcc9e813b125f8a1 to your computer and use it in GitHub Desktop.
7 languages, Haskell, day 1, map-coloring
-- Author: Bart Groeneveld
-- License: GPLv3
module Main where
data Color = Red
| Green
| Blue
deriving (Eq, Show, Enum)
colors = [Red ..]
coloring = head [[("Tennessee" , cr1),
("Mississippi", cr2),
("Alabama" , cr3),
("Georgia" , cr4),
("Florida" , cr5)]
| cr1 <- colors,
cr2 <- colors,
cr3 <- colors,
cr4 <- colors,
cr5 <- colors,
cr1 `notElem` [cr2, cr3, cr4],
cr3 `notElem` [cr2, cr4],
cr5 `notElem` [cr2, cr3, cr4]]
@stil4m
Copy link

stil4m commented Feb 27, 2016

You could make different list comprehensions to solve it more clear. For example:

getC2and4s = [(c2,c4) | cr2 <- colors, cr4 <- colors]

getC234s = [  (c2,c3,c4) | c3 <- colors, (c2,c4) <- getC2and4s, myCheck]

getC12345s = ...

In the end I would map the tuple with 5 colors to the tuples with state names. Something with the signature

colorsWithStates :: (Color,Color,Color,Color,Color) -> (StateColor, StateColor, StateColor,StateColor,StateColor)

To introduce StateColor you should look into the type keyword.

I think you will get the idea. Otherwise you can always contacts me.

@stil4m
Copy link

stil4m commented Feb 27, 2016

One more thing: Try to solve it in parts. You currently are trying to solve it in one big function, which makes it harder to reason about it IMO. Clearly c2 and c4 have no restrictions, thus you can extract those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment