[英]OCaml expression type issue
我正在嘗試創建一個OCaml函數,將一個字符串中的'a'添加到給定的參數中。
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)
我在第4行得到一個錯誤,說'這個表達式有字符串 - > int但是這里用的是int'類型。 我不確定為什么它希望表達式'count_l_in_word initial + 1'是一個int。 它應該真的期望整行'count_l_in_word initial + 1(Str.string_after word 1)'是一個int。
有人能幫忙嗎
count_l_in_word initial+1 (Str.string_after word 1)
被解析為
(count_l_in_word initial) + (1 ((Str.string_after word) 1))
所以你需要添加一些parens:
count_l_in_word (initial + 1) (Str.string_after word 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.