create a histogram OCaml

风格不统一 提交于 2019-12-02 07:36:12

The issue is that xs contains potentially elements that are equal to x. This is what you see in your ouput : (2,3) means that there is 3 times 2 in the list; xs is then equals to [2;2;3;4;4;1]... and so on.

Also (not impacting the conclusion): you have 2 definitions of count, but they are identical.

To implement an histogram, use Hashtbl :

let h = Hashtbl.create 1000;;    
List.iter (fun x -> let c = try Hashtbl.find h x with Not_found -> 0 in Hashtbl.replace h x (c+1)) your_list;;
Hashtbl.fold (fun x y acc ->  (x,y)::acc) h [];;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!