Debian Codegraph @

benchmarks/rnrs-benchmarks/bibfreq.ss

raw
1
2 ;;; find the most frequently referenced word in the bible.
3 ;;; aziz ghuloum (Nov 2007)
4
5 (library (rnrs-benchmarks bibfreq)
6 (export main)
7 (import (rnrs) (rnrs-benchmarks))
8
9 (define (fill h)
10 (let ([p (open-input-file "bib")])
11 (define (put ls)
12 (hashtable-update! h
13 (string->symbol
14 (list->string
15 (reverse ls)))
16 (lambda (x) (+ x 1))
17 0))
18 (define (alpha ls)
19 (let ([c (read-char p)])
20 (cond
21 [(eof-object? c)
22 (put ls)]
23 [(char-alphabetic? c)
24 (alpha (cons (char-downcase c) ls))]
25 [else (put ls) (non-alpha)])))
26 (define (non-alpha)
27 (let ([c (read-char p)])
28 (cond
29 [(eof-object? c) (values)]
30 [(char-alphabetic? c)
31 (alpha (list (char-downcase c)))]
32 [else (non-alpha)])))
33 (non-alpha)
34 (close-input-port p)))
35
36 (define (list-head ls n)
37 (cond
38 [(or (zero? n) (null? ls)) '()]
39 [else (cons (car ls) (list-head (cdr ls) (- n 1)))]))
40
41 (define (go)
42 (let ([h (make-eq-hashtable)])
43 (fill h)
44 (let-values ([(keys vals) (hashtable-entries h)])
45 (let ([ls (map cons
46 (vector->list keys)
47 (vector->list vals))])
48 (list-head
49 (list-sort (lambda (a b) (> (cdr a) (cdr b))) ls)
50 10)))))
51
52
53 (define (main . args)
54 (run-benchmark
55 "bibfreq"
56 bibfreq-iters
57 (lambda (result)
58 (equal? result
59 '((the . 63922) (and . 51696) (of . 34615) (to . 13562) (that . 12913)
60 (in . 12666) (he . 10420) (shall . 9838) (unto . 8997) (for . 8971))))
61 (lambda () (lambda () (go))))))