let length str =
let len = String.length str in
let rec length_helper str curr =
if str = "" then 0 else 1 + length_helper (String.sub str 1 (len - curr - 1)) (curr + 1)
in length_helper str 0;;
We use this code internally at Google. JFC.