What am I doing wrong with Set.Fold F#

后端 未结 2 1281
失恋的感觉
失恋的感觉 2021-01-22 01:18

Colouring problem :

Hello, I\'m trying to implement a bool function that returns true when a color can be extended to a country and false otherwise but I\'m having troub

2条回答
  •  清酒与你
    2021-01-22 01:55

    What about this?

    type Country      = string
    type Neighbours   = Set
    type SharesColour = Set
    
    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"
      |]
    
    []
    let main argv =
      printfn "%A" <| canShareColour neighbours "Estonia" sharesColour
      printfn "%A" <| canShareColour neighbours "Benin"   sharesColour
      0
    

    Changed the names to something that made more sense to me. You might not agree.

提交回复
热议问题