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: 
let m = ["a","b"; "a","c"; "b","d"]

let rec countries =function|[]->[]   |(a,b)::m->a::b:: countries m

let rec rNb a b   =function|[]->false|y::ys   ->y=(a,b)|| y=(b,a) || rNb a b ys

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

let rec xClrng m c=function|[]->[[c]]|cl::cls ->if canXCl m c cl  then (c::cl)::cls  else cl:: xClrng m c cls

List.fold(fun a i->xClrng m i a) [] (countries m|>List.distinct)//[["d";"a"];["c";"b"]]Musa.Jahanghir@Live.com
val m : (string * string) list

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

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

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

Full name: Script.canXCl
val m : ('a * 'a) list (requires equality)
val c : 'a (requires equality)
val l : 'a (requires equality)
val ls : '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)
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
val distinct : list:'T list -> 'T list (requires equality)

Full name: Microsoft.FSharp.Collections.List.distinct

More information

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