OCaml Expression Type Problem

I am trying to make an OCaml function that adds the number 'a to a string to a given argument.

let rec count_l_in_word (initial : int) (word : string) : int= if String.length word = 0 then initial else if word.[0] = 'a' then count_l_in_word initial+1 (Str.string_after word 1) else count_l_in_word initial (Str.string_after word 1) 

I get an error on line 4: "This expression is of type string → int, but here it is used with type int." I'm not sure why it expects the expression 'count_l_in_word initial + 1' as int. This should really expect that the entire string 'count_l_in_word initial + 1 (Str.string_after word 1)' will be int.

Can anyone help with this

+4
source share
1 answer
 count_l_in_word initial+1 (Str.string_after word 1) 

analyzed as

 (count_l_in_word initial) + (1 ((Str.string_after word) 1)) 

so you need to add a few characters:

 count_l_in_word (initial + 1) (Str.string_after word 1) 
+4
source

All Articles