如何压缩列表

问题描述 投票:0回答:0

我有一个列表:

["a"; "a"; "a"; "c"; "b"; "c"; "c"; "a"; "a"; "d"; "x"; "e"; "e"; "e"]
我想要compress。换句话说,删除每个索引左侧和右侧的重复项。

函数compress l应该返回

["a"; "c"; "b"; "a"; "d"; "x"; "e"]

我已经在 OCaml 中尝试过这个功能:

let compress list = List.filteri (fun i el -> compare (List.nth list i) el != 0) list;;

但是它抛出错误Exception:失败“nth”。

filter ocaml ml
© www.soinside.com 2019 - 2024. All rights reserved.