【发布时间】:2011-01-17 02:08:04
【问题描述】:
我正在使用 OCaml 中的列表列表,并且我正在尝试编写一个函数来组合所有共享相同头部的列表。这是我到目前为止所拥有的,我使用了 List.hd 内置函数,但毫不奇怪,我得到了失败的“hd”错误:
let rec combineSameHead list nlist = match list with
| [] -> []@nlist
| h::t -> if List.hd h = List.hd (List.hd t)
then combineSameHead t nlist@uniq(h@(List.hd t))
else combineSameHead t nlist@h;;
例如,如果我有这个列表:
[[Sentence; Quiet]; [Sentence; Grunt]; [Sentence; Shout]]
我想把它组合成:
[[Sentence; Quiet; Grunt; Shout]]
我编写的函数 uniq 只是删除了列表中的所有重复项。请让我知道我将如何完成这项工作。提前致谢!
【问题讨论】:
标签: ocaml