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 delimiters " \t\n")
  15. (define delimiters-set (string->char-set delimiters))
  16. (define count (make-hash-table (inexact->exact 1e6)))
  17. (define (add-word next)
  18. (hash-set! count next
  19. (1+ (hash-ref count next 0))))
  20. (let loop ((next (read-delimited delimiters port)))
  21. (unless (eof-object? next)
  22. (unless (string-every delimiters-set next)
  23. (add-word next))
  24. (loop (read-delimited delimiters port))))
  25. (sort!
  26. (hash-map->list (lambda (key val) (cons val key)) count)
  27. count-or-unicode>?))
  28. (for-each
  29. (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x)))
  30. (count-words (current-input-port)))
  31.