-
- #!/usr/bin/env bash
- # -*- mode: scheme -*-
- exec guile $0
- # !#
-
- (import (ice-9 rdelim)
- (ice-9 hash-table))
-
- (define (count-or-unicode>? a b)
- (let ((numa (car a))(numb (car b)))
- (or (> numa numb)
- (and (= numa numb)
- (string<? (cdr a) (cdr b))))))
-
- (define (count-words port)
- (define count (make-hash-table (inexact->exact 1e6)))
- (define (add-word next)
- (hash-set! count next
- (1+ (hash-ref count next 0))))
- (define delimiters " \t\n")
- (let loop ((next (read-delimited delimiters port)))
- (unless (eof-object? next)
- (add-word next)
- (loop (read-delimited delimiters port))))
- (sort
- (hash-fold (lambda (key val prior) (cons (cons val key) prior)) '() count)
- count-or-unicode>?))
-
- (for-each
- (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x)))
- (count-words (current-input-port)))
-