views:

94

answers:

3

I am supposed to write a scheme function (digit-count n) that accepts a positive integer n and evaluates to the number of digits of n that are 6, 4, or 9.

I am having trouble understanding what exactly I am supposed to do, I am confused about the "digits of n that are 6, 4 or 9", what does this mean?

+4  A: 

This is just an interpretation question, but I would say that you would take the decimal representation of a number, and count the total number of digits that are 6, 4, or 9. For example:

  • 100 --> 0
  • 4 --> 1
  • 469 --> 3
  • 444 --> 3

Get it now?

Mike Axiak
A: 

One interpretation - example:

Given 678799391, the number of digits would be 0 for 4, 1 for 6 and 3 for 9. The sum of the occurences would be 0 + 1 + 3 = 4.

The MYYN
A: 

Convert the whole number to a list and check each one individually.

(define (number->list x) 
  (string->list (number->string x))

(define (6-4-or-9 x) (cond ((= x 6) true)) ((= x 4) true)) ((= x 9) true))))

(define (count-6-4-9 x) (cond ((6-4-or-9 (car (number->list x)))).......

I'm sure you can see where that's going. It's pretty crude, and I'm not sure it's really idiomatic, but it should work.

The idea is is that you convert the number to a list, check to first digit, if it's six, four or nine, recursively call the procedure on the cdr of the number list converted back to a string + 1...

Josh
Your code would look better when formatted.
The MYYN
Better to use `or` instead of `cond`. Even better -- use `member`. Also -- `string->list` returns a list of characters, so you should be used `#\6` etc, not `6`.
Eli Barzilay
Thanks Eli! I'm still kind of new to scheme, I bet you can tell!
Josh