Recursive function to check if a char is a letter

Since you’ve found a solution, I’ll provide my “bad idea” implementation for comparison. Keep in mind that octachrons’s remarks are totally right.

let is_alpha c =
  let i = Char.code c in
    if i >= 65 && i <= 90 || i >= 97 && i <= 122 then true
    else false

let rec letter s =
  let l = String.length s in
    if l = 0 then false
    else
      if not (is_alpha s.[0]) then false
      else
        if l = 1 then true
        else letter (String.sub s 1 (l-1))
1 Like