09-24-2012

posted Sep 24, 2013, 7:54 AM by Samuel Konstantinovich   [ updated Sep 24, 2013, 7:54 AM ]
Lab:
Work on the "simple calculations" problem set from   http://bert.stuy.edu/pbrooks/SchemingBat/scheming.py

(define x 876543)
to extract a digit we can write an expression:
1's place:    (remainder x 10) 
10's place:  (remainder (quotient x 10) 10 )
100's place:  (remainder (quotient x 100) 10 )
1000's place:  (remainder (quotient x 1000) 10 )
rewrite the 1's place:  (remainder (quotient x 1) 10 )   ; so it fits the same pattern!

1,10,100,1000 are powers of 10, so 10^0, 10^1, 10^2,.. 
The exponent corresponds to a parameter we could use to determine the digit we want!!!

(getNthDigit x n) has a general formula   (remainder (quotient x  10^n ) 10) ... so we can rewrite our function and replace 10^n with a scheme function call
Comments