#!/usr/bin/env bash # -*- mode: scheme -*- exec guile $0 # !# (import (ice-9 rdelim) (ice-9 hash-table)) (define (count-or-unicode>? a b) (let ((Na (car a))(Nb (car b))) (or (> Na Nb) (and (= Na Nb) (stringchar-set delimiters)) (define buf (make-string 10000)) (define (read-word port) (let ((res (%read-delimited! delimiters buf #t port))) (or (and (eof-object? (car res)) (car res)) (substring buf 0 (cdr res))))) (define count (make-hash-table (inexact->exact 1e5))) (define (add-word next) (hash-set! count next (1+ (hash-ref count next 0)))) (let loop ((next (read-word port))) (unless (eof-object? next) (unless (string-every delimiters-set next) (add-word next)) (loop (read-word port)))) (sort! ;; destructive sort to save memory (hash-map->list (lambda (key val) (cons val key)) count) count-or-unicode>?)) (for-each (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x))) (count-words (current-input-port)))