【发布时间】:2019-04-16 16:37:06
【问题描述】:
我需要在 OCaml 中编码列表。命令:编码 ['a','a','b','b','b','c'];;必须返回 [(2,'a');(3,'b');(1,'c')]
现在我有了这个功能:
let encode list =
let rec encodeHelper list acc = match list with
| [] -> []
| head :: [] -> (1, head) :: []
| head :: headNext :: tail -> if (head = headNext) then encodeHelper (headNext :: tail) (acc + 1)
else (acc, head) :: encodeHelper (headNext :: tail) acc
in encodeHelper list 1
;;
但它返回:
- : (int * (char * char * char * char * char * char)) 列表 = [(1, ('a', 'a', 'b', 'b', 'b', 'c'))]
【问题讨论】:
标签: function functional-programming ocaml