开发者

OCaml expression type issue

开发者 https://www.devze.com 2023-04-09 08:02 出处:网络
I am trying to make an OCaml function that addsthe number of \'a\'s in a string to a given argument.

I am trying to make an OCaml function that addsthe number of 'a's in a string to a given argument.

let rec 开发者_如何学Pythoncount_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 saying 'This expression has type string -> int but is here used with type int'. I am not sure why it expects the expression 'count_l_in_word initial+1' to be an int. It should really expect the whole line 'count_l_in_word initial+1 (Str.string_after word 1)' to be an int.

Can anyone help with this


count_l_in_word initial+1 (Str.string_after word 1)

is parsed as

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

so you need to add some parens:

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

精彩评论

暂无评论...
验证码 换一张
取 消