spacepaste

  1.  
  2. #!/usr/bin/env bash
  3. exec guile $0
  4. # !#
  5. (import (ice-9 rdelim)
  6. (ice-9 hash-table))
  7. (define (format-entry x)
  8. (format #t "~a ~a\n" (car (cdr x)) (car x)))
  9. (define (count-or-unicode>? a b)
  10. (or (> (car a) (car b))
  11. (and (= (car a) (car b))
  12. (let ((A (car (cdr a)))
  13. (B (car (cdr b))))
  14. (let ((A (if (char? A) (string A) A))
  15. (B (if (char? B) (string B) B)))
  16. (string<? A B))))))
  17. (define (count-words port)
  18. (let ((count (make-hash-table)))
  19. (let ((next (read-char port)))
  20. (while (not (eof-object? next))
  21. (let ((num (or (hash-ref count next) 0)))
  22. (hash-set! count next (+ num 1))
  23. (set! next (read-delimited " \n" (current-input-port))))))
  24. (map format-entry
  25. (sort (hash-fold
  26. (λ (key value prior-result) (cons (list value key) prior-result)) '() count)
  27. count-or-unicode>?))))
  28. (count-words (current-input-port))
  29.