從列表示例[「狗」;「貓」;「狗」;「貓」;「狗」]我必須找到一個單詞產生的時間數這個清單 結果 - > [(「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