【发布时间】:2016-02-14 23:59:13
【问题描述】:
我在尝试将一个元素与集合中的所有元素进行比较时遇到问题。我想编写一个布尔函数,如果元素不是邻居则返回 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 neighbours. 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
*)
我在使用 canBeExtBy 函数时遇到问题。如果这是我的图表和我的 col:
val myWorld : Chart =
set
[("Andorra", "Benin"); ("Andorra", "Canada"); ("Andorra", "Denmark");
("Benin", "Canada"); ("Benin", "Denmark"); ("Canada", "Denmark");
("Estonia", "Canada"); ("Estonia", "Denmark"); ("Estonia", "Finland");
...]
col = set
["Canada"]
如果我调用,我的函数应该返回 false
canBeExtBy col "Denmark" myWorld;;
这是我的代码,我收到一个错误,列在底部。
(* The colour col can be extended by the country ct when they are no neighbours
according to chart.*)
val canBeExtBy :
col:Set<'a> -> ct:'a -> chart:Set<'a * 'a> -> bool when 'a : comparison
*)
错误:
Set.forall(fun x -> areNeighbours x ct) col;;
----------------------^^^^^^^^^^^^^^^^^^
This expression was expected to have type
bool
but here has type
Set<'a * 'a> -> bool
【问题讨论】:
-
您的
areNeighbours函数需要三个参数,但您在 lambda 中只传递了两个(x和ct)。 -
@TeaDrivenDev 您应该将此作为答案发布:)
-
出于好奇,您是 McGill 的 CS 学生吗?
标签: f#