Invalid_argument“String.sub / Bytes.sub”

问题描述 投票:0回答:1

我有,我做练习的一个小问题。我尝试递归计算一个字符串元音,但是我有这个奇怪的错误大跌眼镜。有人可以解释我为什么?

  let rec nb_voyelle = function chaine ->
                           if chaine == "" then
                             0
                           else
                             let length = (String.length chaine)-1 in
                             let p_length = String.sub chaine 0 length in
                             match chaine.[length] with
                             | 'a' | 'e' | 'i' | 'o' | 'u' | 'y' -> 1 + nb_voyelle p_length
                             | _ -> 0 + nb_voyelle p_length

    ;;
recursion types ocaml recurrence
1个回答
2
投票

答案是我用“==”来比较,我最终的情况下,字符串这是不是普遍好于两个元素之间是否相等。

这样,壳体(String.sub“” 0 -1)和发生的功能失效提高此错误。

© www.soinside.com 2019 - 2024. All rights reserved.