Set.forall F#遇到问题 [英] Trouble with Set.forall F#
问题描述
我在尝试将一个元素与集合中的所有元素进行比较时遇到麻烦.我想编写一个布尔函数,如果一个元素不是邻居,则返回true;如果一个元素是邻居,则返回false.我们想给图表上色 因此,共享边界的两个国家/地区不会使用相同的颜色.我会用代码解释
I'm having trouble trying to compare an element to all elements of a set. I want to write a boolean function that returns true if an element is not a neighbour and false if an element is a neighbour. We want to colour the chart so that two countries that share a border are not given the same colour. I will explain it with code
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函数时遇到麻烦.如果这是我的图表和我的列:
I'm having trouble with the canBeExtBy function. If this is my chart and my 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"]
然后,如果我调用
canBeExtBy col "Denmark" myWorld;;
这是我的代码,出现错误,列在底部.
Here is my code, I get an error which is listed at the bottom.
(* 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
推荐答案
听您的类型.
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
中,您仅向其传递了两个Country
值,而不是Chart
.
Instead of a boolean value, there is a function of type Set<'a * 'b> -> bool
. That is a sign for a partially applied function that's missing its last argument of type Set<'a * 'b>
. If you look at your areNeighbours
function, you can see that it takes three arguments, two of type Country
, and one of type Chart
, but in canBeExtBy
, you're only passing it two Country
values, but not the Chart
.
要使其进行编译,canBeExtBy
需要看起来像这样:
To make it compile, canBeExtBy
needs to look like this:
let canBeExtBy col ct chart =
Set.forall(fun x -> areNeighbours x ct chart |> not) col
这篇关于Set.forall F#遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!