In this line:
| x::xs when x=collecting -> temp xs collecting counted+1
the compiler interprets your code as
| x::xs when x=collecting -> (temp xs collecting counted)+1
but you want
| x::xs when x=collecting -> temp xs collecting (counted+1)
However, even with this change, one problem with your algorithm is that the temp function is not tail recursive, which means that it can cause a stack overflow when called in a long list (for example, countoccurences [1..10000] my machine). If this is important to you, you should rewrite your temp helper function so that it is recursive. The easiest way to do this is to add the accumulated list parameter and subsequently cancel the list.
let countoccurences list = match list with | x::xs -> let rec temp list collecting counted acc = match list with | x::xs when x = collecting -> temp xs collecting (counted+1) acc | x::xs -> temp xs x 1 ((collecting, counted)::acc) | [] -> (collecting, counted)::acc temp xs x 1 [] |> List.rev | [] -> []
source share