Scheme Exercises

back to exercise list       previous     next

Group:     Exercise: 19/48 

getNthDigitFrom0:  version of getNthDigit, done recursively and counting the least significant digit as digit 0

Write a function whose signature is
(getNthDigitFrom0 number nthFrom0)
which produces the value of the the nth digit in the number, where the least significant digit (that is, the rightmost digit) is considered digit 0. Use a recursive algorithm with no explicit mention of powers of 10 (in other words, no use of the EXPT function or its equivalent).
For example,
(getNthDigitFrom0 1572638 3) --> 2
(getNthDigitFrom0 1572638 0) --> 8
(getNthDigitFrom0 9876543210 13) --> 0 (since there are implicit zeros on the left)
(getNthDigitFrom0 1234 3) --> 1
Thanks to Tom Ngo for this idea.