Ocaml 这里有没有减少重复的方法?

Ocaml 这里有没有减少重复的方法?,ocaml,Ocaml,我正在创建一个囚徒困境模拟,我有两个函数,计算两个玩家的合作和背叛,并将它们作为元组输出。我看到了很多相似之处,并希望获得一些关于如何将这两个函数合并为另一个函数的指导,以便这两个函数可以调用,这样我就不会重用代码了。我对OCaml非常陌生,所以我正在努力找到一种方法来做到这一点 以下是必要的信息: type action = bool ;; type play = action * action ;; type history = play list let rec count_defec

我正在创建一个囚徒困境模拟,我有两个函数,计算两个玩家的合作和背叛,并将它们作为元组输出。我看到了很多相似之处,并希望获得一些关于如何将这两个函数合并为另一个函数的指导,以便这两个函数可以调用,这样我就不会重用代码了。我对OCaml非常陌生,所以我正在努力找到一种方法来做到这一点

以下是必要的信息:

type action = bool ;;
type play = action * action ;;
type history = play list 

let rec count_defections (history : history) : (int * int) = 
  match history with
  | [] -> (0, 0)
  | play :: rest ->
    match play with
    | (player1, player2) ->
      match count_defections rest with
      | (d1, d2) ->
        if (player1, player2) = (false, false) then (d1 + 1, d2 + 1)
        else if (player1, player2) = (false, true) then (d1 + 1, d2)
        else if (player1, player2) = (true, false) then (d1, d2 + 1)
        else (d1, d2) ;;

let rec count_cooperations (history : history) : (int * int) = 
  match history with
  | [] -> (0, 0)
  | play :: rest ->
    match play with
    | (player1, player2) ->
      match count_cooperations rest with
      | (d1, d2) ->
        if (player1, player2) = (true, true) then (d1 + 1, d2 + 1)
        else if (player1, player2) = (true, false) then (d1 + 1, d2)
        else if (player1, player2) = (false, true) then (d1, d2 + 1)
        else (d1, d2) ;;
我最初的想法是:

let count_common (history : history) : (int * int) =
  match history with
  | [] -> (0, 0)
  | play :: rest ->
    match play with
    | (player1, player2) ->
      match .....

但我真的不知道如何做其余的工作。

这里有一个函数,它分别计算一对中的两部分,计算等于给定值的元素

let count_val_in_pairs value pairs =
    List.fold_left
        (fun (cta, ctb) (a, b) ->
            ((if a = value then cta + 1 else cta),
             (if b = value then ctb + 1 else ctb)))
        (0, 0)
        pairs
您的
count\u缺陷是:

let count_defections history =
    count_val_in_pairs false history
let count_cooperations history =
    count_val_in_pairs true history
您的
合作计数是:

let count_defections history =
    count_val_in_pairs false history
let count_cooperations history =
    count_val_in_pairs true history

这里有一个函数,它分别计算一对中的两部分,计算等于给定值的元素

let count_val_in_pairs value pairs =
    List.fold_left
        (fun (cta, ctb) (a, b) ->
            ((if a = value then cta + 1 else cta),
             (if b = value then ctb + 1 else ctb)))
        (0, 0)
        pairs
您的
count\u缺陷是:

let count_defections history =
    count_val_in_pairs false history
let count_cooperations history =
    count_val_in_pairs true history
您的
合作计数是:

let count_defections history =
    count_val_in_pairs false history
let count_cooperations history =
    count_val_in_pairs true history

请注意,您可以编写
let(d1,d2)=count_defection rest in…
,而不是将
match count_defection rest与|(d1,d2)->…
。此外,您还可以在
play
上进行深度模式匹配,以避免双重匹配,如:
match history with |[]-(0,0)|(player1,player2)::rest->…
请注意,您可以编写
let(d1,d2)=count u defection rest in…
而不是编写
match count u defection rest with |(d1,d2>。您还可以在
play
上进行深度模式匹配,以避免重复匹配,如:
match history with |[]->(0,0)|(player1,player2)::rest->……