function - What am I doing wrong with Set.Fold F# -
colouring problem :
hello, i'm trying implement bool function returns true when color can extended country , false otherwise i'm having trouble working sets cannot pattern match them...
my code :
type country = string;; type chart = set<country*country>;; type colour = set<country>;; type colouring = set<colour>;; (* how tell 2 countries neghbours. requires chart.*) let areneighbours ct1 ct2 chart = set.contains (ct1,ct2) chart || set.contains (ct2,ct1) chart;; (* val areneighbours : ct1:'a -> ct2:'a -> chart:set<'a * 'a> -> bool when 'a : comparison *) (* colour col can extended country ct when no neighbours according chart.*) let canbeextby (col:colouring) (ct:country) (chart:chart) = col |> set.fold (fun x -> (if (areneighbours x ct chart) true else false)) what expected : need check if ct neighbour of country in col (assuming there countries in col), according neighbourhood defined in chart. if
chart = set [("andorra", "benin"); ("andorra", "canada"); ("andorra", "denmark"); ("benin", "canada"); ("benin", "denmark"); ("canada", "denmark"); ("estonia", "canada"); ("estonia", "denmark"); ("estonia", "finland"); ...] and
col = set ["andorra"] then canbeext should return false when when ct = "benin" or "canada" or "denmark" etc... andorra share border countries , cannot coloured in same colour andora.
obviously have type error in canbeextby i'm trying return bool , it's expecting 'a:colouring. don't know how implement it..
thanks !
what this?
type country = string type neighbours = set<country*country> type sharescolour = set<country> let areneighbours (ns : neighbours) (ct1 : country) (ct2 : country) : bool = set.contains (ct1,ct2) ns || set.contains (ct2,ct1) ns let cansharecolour (ns : neighbours) (ct : country) (s : sharescolour) : bool = s |> seq.exists (areneighbours ns ct) |> not let neighbours : neighbours = set [| ("andorra", "benin") ; ("andorra", "canada") ; ("andorra", "denmark"); ("benin" , "canada") ; ("benin" , "denmark"); ("canada" , "denmark"); ("estonia", "canada") ; ("estonia", "denmark"); ("estonia", "finland"); |] let sharescolour : sharescolour = set [| "andorra" |] [<entrypoint>] let main argv = printfn "%a" <| cansharecolour neighbours "estonia" sharescolour printfn "%a" <| cansharecolour neighbours "benin" sharescolour 0 changed names made more sense me. might not agree.
Comments
Post a Comment