OCAML - 将元组添加到元组列表

OCAML - Adding a tuple to a tuple list

我实际上卡在了在元组列表中添加一个元组。 这是我的代码。

let rec start_of_pattern = fun c index acc patterns n -> match patterns with
  | [] -> acc
  | h::t -> start_of_pattern c index [(Char.escaped c, index, h, n)]@acc t (n+1)

我也试过这个:

let rec start_of_pattern = fun c index acc patterns n -> match patterns with
  | [] -> acc
  | h::t -> start_of_pattern c index (Char.escaped c, index, h, n)::acc t (n+1)

我想向 "acc" 添加一个包含 4 个元素(字符串、整数、字符串、整数)的元组。 c 是一个 char,索引是一个 int,acc 通常是元组列表,模式是一个字符串列表和 n 一个 int。

我有这个错误:

first version

second version

希望大家帮帮我。感谢阅读!

OCaml中的函数应用具有高优先级。所以这个表达式:

f x@y z

解析如下:

(f x) @ (y z)

看看你的第一次尝试,在我看来你需要像这样加上括号:

start_of_pattern c index ([(Char.escaped c, index, h, n)]@acc) t (n+1)

我不敢发誓这会解决你所有的问题。但是没有这些括号,看起来 acc 被作为函数调用。