Last active
February 27, 2016 09:43
-
-
Save BartG95/ebf9fcc9e813b125f8a1 to your computer and use it in GitHub Desktop.
7 languages, Haskell, day 1, map-coloring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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]] |
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.
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
I actually want to shorten this part:
to something like
(cr1, cr2, cr3, cr4, cr5) <- colors
. Any suggestions to do that?