I have a function with a recursive function within, to count the letters in words so that "hello" returns [|("h",1);("e",1);("l",2);("o",1);|].
My code is as follows:
let letterCheck (a:string) =
let aList = a |> Seq.toList;
let rec _check (charList:char list) (result:(char * int) array) =
match charList with
| head :: tail ->
if Array.exists (fun (c,i) -> c = head) result then
let index = Array.findIndex (fun (c,i) -> if c = head then true else false) result;
Array.set result index (result.[index].[1]+1);
else
Array.append result [|(head,1)|];
_check tail result;
| [] -> result;
_check aList [||];
but even though I type annotated result it errors on the Array.set line with an error:
Program.fs(73,45): error FS0752: The operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point. Consider adding further type constraints.
How can I fix this error or am I doing something fundamentally wrong?