-
- #!/usr/bin/env bash
- exec guile $0
- # !#
-
- (import (ice-9 rdelim)
- (ice-9 hash-table))
-
- (define (format-entry x)
- (format #t "~a ~a\n" (car (cdr x)) (car x)))
-
- (define (count-or-unicode>? a b)
- (or (> (car a) (car b))
- (and (= (car a) (car b))
- (let ((A (car (cdr a)))
- (B (car (cdr b))))
- (let ((A (if (char? A) (string A) A))
- (B (if (char? B) (string B) B)))
- (string<? A B))))))
-
- (define (count-words port)
- (let ((count (make-hash-table)))
- (let ((next (read-char port)))
- (while (not (eof-object? next))
- (let ((num (or (hash-ref count next) 0)))
- (hash-set! count next (+ num 1))
- (set! next (read-delimited " \n" (current-input-port))))))
- (map format-entry
- (sort (hash-fold
- (λ (key value prior-result) (cons (list value key) prior-result)) '() count)
- count-or-unicode>?))))
-
- (count-words (current-input-port))
-