| 1 |
;;; WC -- One of the Kernighan and Van Wyk benchmarks. |
| 2 |
|
| 3 |
(library (rnrs-benchmarks wc) |
| 4 |
(export main) |
| 5 |
(import (rnrs) (rnrs-benchmarks)) |
| 6 |
|
| 7 |
(define inport #f) |
| 8 |
|
| 9 |
(define nl #f) |
| 10 |
(define nw #f) |
| 11 |
(define nc #f) |
| 12 |
(define inword #f) |
| 13 |
|
| 14 |
(define (wcport port) |
| 15 |
(let ((x (read-char port))) |
| 16 |
(if (eof-object? x) |
| 17 |
(begin |
| 18 |
(list nl nw nc)) |
| 19 |
(begin |
| 20 |
(set! nc (+ nc 1)) |
| 21 |
(if (char=? x #\newline) |
| 22 |
(set! nl (+ nl 1))) |
| 23 |
(if (or (char=? x #\space) |
| 24 |
(char=? x #\newline)) |
| 25 |
(set! inword #f) |
| 26 |
(if (not inword) |
| 27 |
(begin |
| 28 |
(set! nw (+ nw 1)) |
| 29 |
(set! inword #t)))) |
| 30 |
(wcport port))))) |
| 31 |
|
| 32 |
|
| 33 |
(define (go) |
| 34 |
(set! inport (open-input-file "bib")) |
| 35 |
(set! nl 0) |
| 36 |
(set! nw 0) |
| 37 |
(set! nc 0) |
| 38 |
(set! inword #f) |
| 39 |
(let ((result (wcport inport))) |
| 40 |
(close-input-port inport) |
| 41 |
result)) |
| 42 |
|
| 43 |
(define (main . args) |
| 44 |
(run-benchmark |
| 45 |
"wc" |
| 46 |
wc-iters |
| 47 |
(lambda (result) (equal? result '(31102 851820 4460056))) |
| 48 |
(lambda () (lambda () (go)))))) |