From 9487a13344360c93bf63a7859a2336a074f43cae Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Thu, 14 Oct 2010 08:04:03 -0600 Subject: [PATCH] typed --- benford.rkt | 72 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/benford.rkt b/benford.rkt index c49c46e0..e6cae0a4 100644 --- a/benford.rkt +++ b/benford.rkt @@ -1,37 +1,49 @@ #lang typed/racket +(define-type Inputs (Listof Real)) +(define-type Digits (Listof Integer)) +(define-type Frequencies (Listof (Pair Integer Number))) + +(: freq (Digits -> Frequencies)) (define (freq l) (define h (make-vector 10 0)) (define tot - (for/fold ([tot 0]) ([e (in-list l)]) - (dict-update! h e add1) - (add1 tot))) - (for/list ([(e c) (in-dict h)]) - (cons e (/ c tot)))) -(define nums->digits - (curry map - (compose string->number - string - (λ (x) (string-ref x 0)) - number->string - (λ (x) (if (x . < . 0) (- x) x))))) -(define (sk l) + (for/fold: + : Integer ([tot : Integer 0]) + ([e : Integer (in-list l)]) + (vector-set! h e (add1 (vector-ref h e))) + (add1 tot))) + (for/list: : Frequencies + ([e : Integer (in-range 10)] + [c : Integer (in-vector h)]) + (cons e (/ c tot)))) + +(: nums->digits (Inputs -> Digits)) +(define (nums->digits l) + (for/list: : Digits + ([x : Real (in-list l)]) + (define z (if (x . < . 0) (- x) x)) + (- (char->integer + (string-ref (number->string z) 0)) + (char->integer #\0)))) + +(: benford (Inputs -> Frequencies)) +(define (benford l) (freq (nums->digits l))) -(define (sort-em l) - (sort l >= #:key cdr)) (equal? - (sort-em - (sk (list 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.10 - 11 12 13 -14 15 16 17 18 19 - 21 -22 23 24 25 26 27 28 - 31 32 -33 34 35 36 37 - 41 42 43 44 45 46 - 51 -52 54 54 55 - 61 62 -63 64 - 71 -72 73 - 81 82 - 91))) - (for/list ([i (in-range 10)]) - (cons i - (/ (- 10 i) - (+ 10 9 8 7 6 5 4 3 2 1))))) \ No newline at end of file + (benford + (list 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.10 + 11 12 13 -14 15 16 17 18 19 + 21 -22 23 24 25 26 27 28 + 31 32 -33 34 35 36 37 + 41 42 43 44 45 46 + 51 -52 54 54 55 + 61 62 -63 64 + 71 -72 73 + 81 82 + 91)) + (for/list: : Frequencies + ([i : Integer (in-range 10)]) + (cons i + (/ (- 10 i) + (+ 10 9 8 7 6 5 4 3 2 1))))) \ No newline at end of file