logoalt Hacker News

sparkie • today at 8:59 AM • 0 replies • view on HN

Think of it like a recursive process which terminates for 0, but produces an infinite recursion for anything else.

    repeat_digit(x) = x / 10 + repeat_digit(x / 10)
Since 0 / 10 = 0, 0.0 / 10 = 0, and 0.000 / 10 = 0, and 0 + 0.0 + 0.000 = 0, etc - we know that this produces 0 and could short circuit it to terminate - maybe clearer instead to write:

    repeat_digit(x) =
        if x == 0 then 0
        else x / 10 + repeat_digit(x / 10)

    repeat_digit(0) = 0        ;; terminates
    repeat_digit(9) = 0.999... ;; never terminates