spacepaste

  1.  
  2. #!/usr/bin/env bash
  3. # -*- mode: scheme -*-
  4. exec guile $0
  5. # !#
  6. (import (ice-9 rdelim)
  7. (ice-9 hash-table))
  8. (define (count-or-unicode>? a b)
  9. (let ((numa (car a))(numb (car b)))
  10. (or (> numa numb)
  11. (and (= numa numb)
  12. (string<? (cdr a) (cdr b))))))
  13. (define (count-words port)
  14. (define count (make-hash-table (inexact->exact 1e6)))
  15. (define (add-word next)
  16. (hash-set! count next
  17. (1+ (hash-ref count next 0))))
  18. (define delimiters " \t\n")
  19. (let loop ((next (read-delimited delimiters port)))
  20. (unless (eof-object? next)
  21. (add-word next)
  22. (loop (read-delimited delimiters port))))
  23. (sort
  24. (hash-fold (lambda (key val prior) (cons (cons val key) prior)) '() count)
  25. count-or-unicode>?))
  26. (for-each
  27. (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x)))
  28. (count-words (current-input-port)))
  29.