该表达式具有类型(String.t * String.t *'a)list - > 'b但是表达式是'c list OCAML'

时间:2017-05-15 12:43:38

标签: list tuples ocaml

大家好,我有这个错误信息,我找不到办法解决这个问题......

这是我的功能:

let station gares dist =
let rec station2 firstT secondT index gares final = function
  | [] -> final
  | tete::reste when (String.equal (getFirstTown tete) firstT) == true
      && (String.equal (getSecondTown tete) secondT) == true -> if (index + 1) < (List.length gares) then
          station2 (List.nth gares index) (List.nth gares (index + 1)) (index + 1) gares final@[(firstT, secondT, (getDistance tete))] reste
        else final
  | tete::reste when (String.equal (getFirstTown tete) firstT) == false
      || (String.equal (getSecondTown tete) secondT) == false -> station2 firstT secondT index gares final reste
  | _::_ -> final
in station2 (List.nth gares 0) (List.nth gares 1) 1 gares [] dist

这是我的功能的签名:

val station : string list -> (string * string * int) list -> (string * string * int) list

我认为我的错误来自于我试图在我的最终列表中添加新元组的部分:

final@[(firstT, secondT, (getDistance tete))]

我在这一点上迷失了,我觉得我已经尝试了一切......

1 个答案:

答案 0 :(得分:0)

这来自函数应用程序和(@)运算符之间的优先级差异。

这一行:

station2 (List.nth gares index) (List.nth gares (index + 1))
  (index + 1) gares final@[(firstT, secondT, (getDistance tete))] reste

被解析为

(station2 (List.nth gares index) (List.nth gares (index + 1))
  (index + 1) gares final)@([(firstT, secondT, (getDistance tete))] reste)

由于您要将新列表作为参数传递,请使用括号:

station2 (List.nth gares index) (List.nth gares (index + 1))
  (index + 1) gares (final@[(firstT, secondT, (getDistance tete))]) reste

或者更好的是,提取一个let-binding:

let final2 = final@[(firstT, secondT, (getDistance tete))] in
station2 (List.nth gares index) (List.nth gares (index + 1))
  (index + 1) gares final2 reste