I have a method that returns the count of the most common element in a list (specifically a list of char values)
Here's my code:
let getMostFrequentCharCount li =
let hashTable = Hashtbl.create (List.length li)
in
let rec getMostFrequentCharCount li acc =
match li with
[] -> acc
| head::tail ->
(match (Hashtbl.find hashTable head) with
exception Not_found -> Hashtbl.add hashTable head 1
| _ -> let currentFreq = Hashtbl.find hashTable head
in
Hashtbl.replace hashTable head (currentFreq+1));
let currentFreq = Hashtbl.find hashTable head
in
if currentFreq > acc
then
getMostFrequentCharCount tail currentFreq
else
getMostFrequentCharCount tail acc
in
getMostFrequentCharCount li 0;;
For some reason, when I remove the parentheses surrounding the second pattern matching block (starts with match (Hashtbl.find hashTable head) with
), my compiler complains that my accumulator acc
has type unit in the next if-statement if currentFreq > acc
when acc
should have type int
.
Why are the parentheses fixing this error?