[racket] a small programming exercise

From: Noel Welsh (noelwelsh at gmail.com)
Date: Thu Oct 14 09:19:17 EDT 2010

The issues are:

  - Converting characters to integers. My solution may not work in all encodings

  - Storing the counts. I used mutation.


(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
                    (add1 (vector-ref counts first-digit))))

  (for ([elt (in-vector counts)]
        [i (in-naturals)])
       (printf "Numbers beginning with ~a: ~a\n" i elt)))

 (list 0 1 2 3 4 5 6 7 8 9 10))


Posted on the users mailing list.