04/08/20 01:49
>>208
最初に書いたように、 OCaml にはハッシュテーブルがあるので、普通にやる
場合にはこれを使うのが普通でしょう。ただ、あまり関数的ではないアルゴリ
ズムになります(下記)。
ちなみに OCaml にも配列はあり、破壊的な代入が可能です。理論の詳しい話
は知りませんが……。
let number2 lst =
let rec main tbl = function
| [] -> tbl
| w::ws ->
if Hashtbl.mem tbl w then
let c = Hashtbl.find tbl w in
Hashtbl.replace tbl w (c+1)
else
Hashtbl.add w 1;
number2 tbl ws
in
main (Hashtbl.create 100) lst
;;