Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let freq str =
- let len = String.length str in
- let freqs = ref [ref ('a', 0)] in
- for i=0 to (len-1) do
- let tempchar = String.get str i in
- try
- let ch = (List.find (fun x-> (fst !x) == tempchar) !freqs) in
- ch := ((fst !ch), (snd !ch)+1)
- with
- Not_found -> freqs:= !freqs@[ref (tempchar, 1)]
- done;
- !freqs
- ;;
- (*
- # freq "i hope this shit works!";;
- - : (char * int) ref list =
- [{contents = ('i', 3)}; {contents = (' ', 4)}; {contents = ('h', 3)};
- {contents = ('o', 2)}; {contents = ('p', 1)}; {contents = ('e', 1)};
- {contents = ('t', 2)}; {contents = ('s', 3)}; {contents = ('w', 1)};
- {contents = ('r', 1)}; {contents = ('k', 1)}; {contents = ('!', 1)}]
- #
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement