从列表示例[“狗”;“猫”;“狗”;“猫”;“狗”]我必须找到一个单词产生的时间数这个清单 结果 - > [(“dog”,3);(“cat”,2)] 但我得到一个奇怪的结果:[(“dog”,1); (“猫”,1); (“狗”,2); (“猫”,2); ( “狗”,3)]Ocaml - 查找列表中的字数
我的代码是这样的:
let rec nuovaParola par l =
match l with
[] -> true
|(a,_)::z ->if (par=a) then false
else nuovaParola par (List.tl l);;
let rec contaParole par l =
let rec contatore par l cont =
match l with
[] -> (par, cont)
|x::y -> if(par=x) then contatore par y (cont+1)
else contatore par y cont
in contatore par l 0;;
let rec occorrenze l =
let rec aux l l1=
match l with
[] -> l1
|x::y -> if (nuovaParola x l1) then aux y [email protected][(contaParole x l)]
else aux y l1
in aux l [];;`
nuovaParola给我true,如果字是不是里面别的名单给我假的。
contaParola用(“单词”,数字)重新激活元组;
Occorrenze是主要功能。我没有发现问题! 感谢您的帮助!
[创建直方图OCaml]的可能副本(http://stackoverflow.com/questions/40442527/create-a-histogram-ocaml) – coredump