This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
typed
- Loading branch information
1 parent
c2c43d4
commit 9487a13
Showing
1 changed file
with
42 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))) | ||
(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))))) |