sizeofInt :: Int -> Int
sizeofInt 0 = 0
sizeofInt x = 1 + (sizeofInt x `div` 10)
Run Code Online (Sandbox Code Playgroud)
除非我把括号括起来,为什么这个功能不会终止x `div` 10?
更新:修复数字为0时的情况
sizeofInt :: Int -> Int
sizeofInt x = if m == 0 then 1
else 1 + sizeofInt m
where m = x `div` 10
Run Code Online (Sandbox Code Playgroud)