5 people like it.

Optimal Cloud Coloring & famous 4 color map

Four colors will print a map so adjacent states have different color. F# has clear concise code. Map has a list of pairs sharing a border. Incremental improvements in the code can be less use of key word 'function'. Since syntax is not ambiguous can it be fixed in the compiler? so '=function |' changes to '=|' ? Welcome to improve. Harrop,Mathias,Jack,Art,HR,fwaris,Don Syme, sffs... Thanks! Musa.Jahanghir@Live.com AVCLive1.com

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
let rec isMember x=function| y::ys -> x=y || (isMember x ys) |[]-> false
let rNb a b m     =isMember (a,b) m || isMember (b,a) m

let addi  x ys    =if isMember x ys then ys else x::ys
let rec countries =function|[]->[]  |(a,b)::m->addi a (addi b (countries m))

let rec canXCl m c=function|[]->true|cl::cls->not(rNb cl c m) && canXCl m c cls

let rec xClrng m c=function|[]->[[c]]|cl::cls->if canXCl m c cl  
                                               then (c::cl)::cls
                                               else cl:: xClrng m c cls
let m = ["a","b"; "a","c"; "b","d"]

List.fold(fun A i ->  xClrng m i A ) [] (countries m)  //[["d";"a"]; ["b";"c"]]  Musa.Jahanghir@Live.com
val isMember : x:'a -> _arg1:'a list -> bool (requires equality)

Full name: Script.isMember
val x : 'a (requires equality)
val y : 'a (requires equality)
val ys : 'a list (requires equality)
val rNb : a:'a -> b:'a -> m:('a * 'a) list -> bool (requires equality)

Full name: Script.rNb
val a : 'a (requires equality)
val b : 'a (requires equality)
val m : ('a * 'a) list (requires equality)
val addi : x:'a -> ys:'a list -> 'a list (requires equality)

Full name: Script.addi
val countries : _arg1:('a * 'a) list -> 'a list (requires equality)

Full name: Script.countries
val canXCl : m:('a * 'a) list -> c:'a -> _arg1:'a list -> bool (requires equality)

Full name: Script.canXCl
val c : 'a (requires equality)
val cl : 'a (requires equality)
val cls : 'a list (requires equality)
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val xClrng : m:('a * 'a) list -> c:'a -> _arg1:'a list list -> 'a list list (requires equality)

Full name: Script.xClrng
val cl : 'a list (requires equality)
val cls : 'a list list (requires equality)
val m : (string * string) list

Full name: Script.m
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val A : string list list
val i : string
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7TW
Posted:6 years ago
Author:Musa Jahanghir
Tags: #computationalgeometry