#!/usr/local/bin/guile \
-e main -s
!#
;;; $Id: wordfreq.guile,v 1.2 2001/06/29 23:12:37 doug Exp $
;;; http://www.bagley.org/~doug/shootout/
;;; from Brad Knotwell
(use-modules (ice-9 string-fun) (ice-9 common-list))
(use-modules (ice-9 format))
(define my-hash (make-hash-table 4000))
(define (print-sorted-hash)
(define (display-elt elt)
(display (format "~7D\t~a\n" (car elt) (cdr elt))))
(map display-elt
(sort-list (hash-fold (lambda (x y z) (cons (cons y x) z)) '() my-hash)
(lambda (x y) (or (> (car x) (car y))
(and (= (car x) (car y))
(string>=? (cdr x) (cdr y))))))))
(define (load-hash x . tl)
(define (do-entry entry)
(let ((entry-val (hash-ref my-hash entry)))
(hash-set! my-hash entry (if entry-val (1+ entry-val) 1))))
(map do-entry (remove-if (lambda (x) (string=? x "")) (cons x tl))))
(define (main args)
(do ((line (read-line) (read-line)))
((eof-object? line) (print-sorted-hash))
(separate-fields-discarding-char
#\space
(list->string (map (lambda (x) (if (char-alphabetic? x) x #\space))
(string->list (string-downcase line))))
load-hash)))