Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement 如何将列表的元组与值进行比较?_If Statement_Design Patterns_Types_F# - Fatal编程技术网

If statement 如何将列表的元组与值进行比较?

If statement 如何将列表的元组与值进行比较?,if-statement,design-patterns,types,f#,If Statement,Design Patterns,Types,F#,我试图比较列表中所有元素的颜色元组。如果颜色相同,则返回true。如果不返回false。当我运行代码时,编译器抛出一个错误 我使用了模式匹配加上嵌套的if语句来检查列表头的颜色是否与所有其他元素匹配 type Suit = Clubs | Diamonds | Hearts | Spades type Rank = Jack | Queen | King | Ace | Num of int type Card = Rank * Suit type Color = Red | Black typ

我试图比较列表中所有元素的颜色元组。如果颜色相同,则返回true。如果不返回false。当我运行代码时,编译器抛出一个错误

我使用了模式匹配加上嵌套的if语句来检查列表头的颜色是否与所有其他元素匹配

type Suit = Clubs | Diamonds | Hearts | Spades
type Rank = Jack | Queen | King | Ace | Num of int
type Card = Rank * Suit
type Color = Red | Black
type Move = Discard of Card | Draw


let cards = [(Jack,Clubs); (Num(8),Spades)]

let card_color (c:Card) = 
  match c with
    | _, Clubs -> Black
    | _, Spades -> Black
    | _, Diamonds -> Red
    | _, Hearts -> Red

let all_same_color cs = 
  match cs with
    | [] -> true
    | x::xs -> 
      if not card_color cs.Head = card_color x then false else true
all_same_color cards

若head和其他元素的颜色匹配,我希望返回true,否则返回false。F#抛出错误:此值不是函数,无法应用。

您可以将最后一条规则写入
|x::xs->card_color cs.Head=card_color x

但我建议编写与

let all_same_color cs =
  cs |> List.forall (fun x -> card_color x = card_color cs.Head)
错误为第21行-->如果不是card_color cs.Head=card_color x,则为false,否则为true