-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_04_ex_20.fs
More file actions
56 lines (47 loc) · 2.02 KB
/
Chapter_04_ex_20.fs
File metadata and controls
56 lines (47 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Chapter_04_ex_20
#if INTERACTIVE
#r "packages/Unquote.2.2.2/lib/net40/unquote.dll"
#r "packages/NUnit.2.6.2/lib/nunit.framework.dll"
#endif
open System
open NUnit.Framework
open Swensen.Unquote
open Chapter_04_ex_19
// 4.20
// I can't help wondering whether functional programmers in general are:
// 1. lazy - I mean how hard is it to write e.g colorMap map - instead of colMap m ?? ;)
// 2. a bit aloof and reveling in slightly unreadable code ?? ;)
// I think the code gets vastly more readable by using good names:
let colorMap map =
let areNeighbors map country1 country2 =
match map with
| (countryX, countryY)::countries ->
(country1 = countryX && country2 = countryY) ||
(country2 = countryX && country1 = countryY)
| [] -> false
// I'm still not fond of the ' naming here, but haven't come up with
// something better, yet...
// What's wrong with the ' ? Its hard to pronounce (doesn't really flow)
// and it doesn't add any information to what the value is.
let rec canColorBeExtendedBy map color country =
match color with
| [] -> true
| country'::color' ->
not(areNeighbors map country' country) &&
canColorBeExtendedBy map color' country
let rec extendColoring map colorings country =
match colorings with
| [] -> [[country]]
| coloring::colorings' ->
if canColorBeExtendedBy map coloring country then
(country::coloring)::colorings'
else
coloring::extendColoring map colorings' country
let addElement x ys = if isMember x ys then ys else x::ys
let rec countries = function
| [] -> []
| (country1, country2)::rest -> addElement country1 (addElement country2 (countries rest))
let rec colorCountries map = function
| [] -> []
| country::rest -> extendColoring map (colorCountries map rest) country
colorCountries map (countries map)