【发布时间】:2016-05-18 12:14:20
【问题描述】:
着色问题:
您好,我正在尝试实现一个 bool 函数,当颜色可以扩展到一个国家/地区时返回 true,否则返回 false,但是我在使用集合时遇到了问题,因为我们无法对它们进行模式匹配...
我的代码:
type Country = string;;
type Chart = Set<Country*Country>;;
type Colour = Set<Country>;;
type Colouring = Set<Colour>;;
(* This is how you tell that two countries are neghbours. It requires a 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
*)
(* The colour col can be extended by the country ct when they are no neighbours
according to chart.*)
let canBeExtBy (col:Colouring) (ct:Country) (chart:Chart) = col |> Set.fold (fun x -> (if (areNeighbours x ct chart) then true else false))
预期结果:根据图表中定义的邻域,我们需要检查 ct 是否是 col 中任何国家的邻居(假设 col 中有国家)。 所以如果
chart = set
[("Andorra", "Benin"); ("Andorra", "Canada"); ("Andorra", "Denmark");
("Benin", "Canada"); ("Benin", "Denmark"); ("Canada", "Denmark");
("Estonia", "Canada"); ("Estonia", "Denmark"); ("Estonia", "Finland");
...]
还有
col = set
["Andorra"]
那么当ct = "Benin" or "Canada" or "Denmark" etc... 安道尔与这些国家接壤时,canBeExt 应该返回 false,因此它们的颜色不能与安道尔相同。
显然我在 canBeExtBy 中有一个类型错误,因为我试图返回一个 bool 并且它期待 'a:Colouring. 我不知道如何实现它..
感谢您的帮助!
【问题讨论】:
-
canBeExtBy应该返回哪个值(什么类型)?你能提供一些预期的测试用例,以及它们的预期返回值吗? -
传递给
Set.fold的函数的类型应该是'State -> 'T -> 'State。这意味着 两个(咖喱)函数参数,但您的匿名函数只接受一个参数。 -
您可以将
(if (areNeighbours x ct chart) then true else false)缩短为areNeighbours x ct chart
标签: function f# functional-programming set