

Subject:
DRScheme problem need quick answers to see, if I understand the way to answer !
Category: Computers > Programming Asked by: kenobobga List Price: $20.00 
Posted:
19 Oct 2005 22:38 PDT
Expires: 18 Nov 2005 21:38 PST Question ID: 582452 
Please include a comment with each solution indicating the amount of time spent solving it. Note: Using the two access functions of (unitsdigit) and (allbutunitsdigit) and/or substitution or recursion functions. (decimallength n) ; returns the number of digits in the number. EXAMPLE: (decimallength 348567) > 6 (ithdigit n i) ; returns digit which is the ith from the left. EXAMPLE: (ithdigit 471835629 4) > 8 (leadingdigit n) ; returns the first digit. EXAMPLE: (leadingdigit 5394) > 5 (occurance d n) ; returns the number of times digit d occurs in n. EXAMPLE: (occurance 6 549457654987587654655478563) > 4 (digitsum n) ; returns the sum of the digits in n. EXAMPLE: (digitsum 2354) > 14, (digitsum 8) > 8 (digitalroot n) ; returns the result of repeatedly applying digitsum until the result is a single digit. EXAMPLE: (digitalroot 341943) > (via 3+4+1+9+4+3=4, then 2+4=6) (backwards n) ; returns the digits in reverse order. You do not need to handle leading or trailing zeros. For this problem you may use multiplication. EXAMPLE: (backwards 329145) > 541923 

Subject:
Re: DRScheme problem need quick answers to see, if I understand the way to answe
Answered By: leapinglizardga on 20 Oct 2005 00:56 PDT 
Dear kenobob, Here you go. (define (decimallength n) ; 4 minutes (if (< n 10) 1 (+ 1 (decimallength (allbutunitsdigit n))))) (define (ithdigit n i) ; 3 minutes (if (= i (decimallength n)) (unitsdigit n) (ithdigit (allbutunitsdigit n) i))) (define (leadingdigit n) ; 1 minute (ithdigit n 1)) (define (occurrence d n) ; 6 minutes (+ (if (= (unitsdigit n) d) 1 0) (if (< n 10) 0 (occurrence d (allbutunitsdigit n))))) (define (digitsum n) ; 2 minutes (+ (unitsdigit n) (if (< n 10) 0 (digitsum (allbutunitsdigit n))))) (define (digitalroot n) ; 1 minute (let ((x (digitsum n))) (if (< x 10) x (digitalroot x)))) (define (backwards n) ; 8 minutes (letrec ((backward (lambda (n part) (if (= n 0) part (backward (allbutunitsdigit n) (+ (* 10 part) (unitsdigit n))))))) (backward n 0))) If you don't already have definitions for unitsdigit and allbutunitsdigit, you can insert the following before all the above definitions. (define (unitsdigit x) (modulo x 10)) (define (allbutunitsdigit x) (/ ( x (unitsdigit x)) 10)) Regards, leapinglizard 

There are no comments at this time. 
If you feel that you have found inappropriate content, please let us know by emailing us at answerssupport@google.com with the question ID listed above. Thank you. 
Search Google Answers for 
Google Home  Answers FAQ  Terms of Service  Privacy Policy 