Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- exec guile $0
- # !#
- (import (ice-9 rdelim)
- (rnrs io ports)
- (ice-9 hash-table))
- (define (count-or-unicode>? a b)
- (or (> (car a) (car b))
- (and (= (car a) (car b))
- (string<? (cdr a) (cdr b)))))
- (define buf (make-string 1000))
- (define (read-word port)
- (define res (%read-delimited! " \n\t" buf #t port))
- (cond
- [(eof-object? (car res))
- (eof-object)]
- [else (substring buf 0 (cdr res))]))
- (define (count-words port)
- (define count (make-hash-table))
- (let loop ([next (read-word port)])
- (unless (eof-object? next)
- (hash-set! count next (+ (hash-ref count next 0) 1))
- (loop (read-word port))))
- (sort
- (hash-fold (lambda (key val prior) (cons (cons val key) prior)) '() count)
- count-or-unicode>?))
- (for-each
- (lambda (x) (display (string-append (number->string (car x)) "\t" (cdr x) "\n")))
- (count-words (current-input-port)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement