Set.forall F#的麻烦

时间:2016-02-14 23:59:14

标签: f#

我在尝试将元素与集合的所有元素进行比较时遇到了麻烦。我想编写一个布尔函数,如果元素不是邻居则返回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  

1 个答案:

答案 0 :(得分:2)

倾听你的类型。

This expression was expected to have type
    bool    
but here has type
    Set<'a * 'a> -> bool

而不是布尔值,有一个Set<'a * 'b> -> bool类型的函数。这是部分应用函数的一个标志,它缺少类型Set<'a * 'b>的最后一个参数。如果您查看areNeighbours函数,可以看到它需要三个参数,两个类型为Country,另一个类型为Chart,但在canBeExtBy中,您和# 39;仅传递两个Country值,但不传递Chart

要使其编译,canBeExtBy需要如下所示:

let canBeExtBy col ct chart =
    Set.forall(fun x -> areNeighbours x ct chart |> not) col