Last active
February 15, 2016 21:22
-
-
Save hakelimopu/db7bf655a284fa9e85ef to your computer and use it in GitHub Desktop.
This file contains 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
type Country = string | |
type Chart = Set<Country*Country> | |
type Colour = Set<Country> | |
type Colouring = Set<Colour> | |
let areNeighbours (chart:Chart) (firstCountry:Country) (secondCountry:Country) :bool = | |
chart.Contains((firstCountry,secondCountry)) || chart.Contains((secondCountry,firstCountry)) | |
let canBeExtBy (chart:Chart) (colour:Colour) (country:Country) :bool = | |
colour | |
|> Set.forall(fun candidate -> candidate |> areNeighbours chart country |> not) | |
let extColouring (chart:Chart) (colouring:Colouring) (country:Country) = | |
let newColouring, wasAdded = | |
colouring | |
|> Set.fold(fun (current,flag) colour -> | |
if flag || canBeExtBy chart colour country |> not then | |
(current |> Set.add colour, flag) | |
else | |
(current |> Set.add (colour |> Set.add country), true)) (Set.empty,false) | |
if wasAdded then | |
newColouring | |
else | |
newColouring | |
|> Set.add (country |> Set.singleton) | |
let countries (chart:Chart) :Colour = | |
(chart |> Set.map(fun (country,_)->country),chart |> Set.map(fun (_,country)->country)) | |
||> Set.union | |
let colCntrs (chart:Chart) (countries:Set<Country>) = | |
countries | |
|> Set.fold (fun current country->extColouring chart current country) Set.empty | |
let colMap chart = colCntrs chart (countries chart) | |
let exMap = | |
[("Andorra", "Benin"); ("Andorra", "Canada"); ("Andorra", "Denmark"); | |
("Benin", "Canada"); ("Benin", "Denmark"); ("Canada", "Denmark"); | |
("Estonia", "Canada"); ("Estonia", "Denmark"); ("Estonia", "Finland")] | |
|> Set.ofList | |
colMap exMap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Opinionated "corrections" and comments below....