[racket] a small programming exercise
The issues are:
- Converting characters to integers. My solution may not work in all encodings
- Storing the counts. I used mutation.
N.
(define (count-first-digit lst)
(define counts (make-vector 10 0))
(for ([elt (in-list lst)])
;; Relies on the char representation of the number
;; being continuous and starting from 0
(define first-digit
(- (char->integer (string-ref (number->string elt) 0))
(char->integer #\0)))
(vector-set! counts
first-digit
(add1 (vector-ref counts first-digit))))
(for ([elt (in-vector counts)]
[i (in-naturals)])
(printf "Numbers beginning with ~a: ~a\n" i elt)))
(count-first-digit
(list 0 1 2 3 4 5 6 7 8 9 10))
N.