You're talking about digits and integer, but to me, as an non-lisper, it looks as if you operate on Strings.
- I would take the number, modulo 10, print that digit.
- If the rest is > 0 recursively call the function with the (number - digit) / 10.
In most languages with integer-arithmetic you can omit the subtraction, since 37/10 => 3 :==: (37-7)/10 => 3
In scala it would look like this:
def redigit (n: Int, sofar: Int = 0) : Int = {
if (n == 0) sofar else
redigit (n / 10, sofar * 10 + n % 10)}
redigit (123)
321
It uses default arguments. First tests with DrScheme didn't succeed. Here is what I came up with:
;; redigit : number number -> number
(define (redigit in sofar)
(cond [(< in 1) sofar]
[else (redigit (/ in 10) (+ (* 10 sofar) (modulo in 10)))])
)
(redigit 134 0)
But the division is precise and doesn't cut off the digits behind the floting point. I get this error:
modulo: expects type as 1st
argument, given: 67/5; other arguments
were: 10
I looked for a function toInt, asInt, floor, round and so on for the term (/ in 10) but I didn't find something useful. Maybe you know it yourself?