【发布时间】:2015-10-31 02:26:06
【问题描述】:
我有一个正在做的项目,我需要在 OCaml 中实现一个直方图函数。我必须编写一个函数,该函数将列表作为参数并以元组列表的形式返回直方图。它看起来像这样:
histogram [1;2;3;1;1;3];;
[(1,3);(2,1);(3,2)]
但是我不能让它工作。我觉得我已经很接近了,但我只需要一些关于如何完成实际直方图函数的帮助/指导。到目前为止,我已经让它检查列表 (nl) 中可能已经存在的任何内容。如果它不包含数字,我将它添加到列表中。我的问题是,一旦我将它添加到列表中,我不知道如何调用该功能。请参阅我的代码以了解问题所在。
let check a ls = match (a,ls) with
|a,[] -> false
|a,xs -> if fst (hd xs) != a then check a (tl xs) else true
let rec count a ls = match ls with
|[] -> 0
|x::xs -> if x = a then 1 + count a xs else 0 + count a xs
let nl = []
let rec histo l = match l with
|[]-> []
|x::xs -> if check x nl then histo xs else nl @ [(x,count x l)] *******
我需要重新调用 ****** 在哪里。任何帮助都将不胜感激。
【问题讨论】: