When encountering a programming language, I often ask the question:
How strlen, which computes the length of a given C-style string, should be implemented
in this language?
Here is a way to implement strlen safely in ATS:
fun
strlen{n:nat}
(
str: string(n)
) : int(n) = let
//
fun loop{i,j:nat}
(str: string(i), j: int(j)): int(i+j) =
if isneqz (str) then loop (str.tail, succ(j)) else j
//
in
loop (str, 0)
end // end of [strlen]
If a language does not allow you to implement strlen, then the language is probably not well-suited
for low-level programming.
However, most languages suited for low-level programming usually do not allow you to implement strlen
safely.