Advertisement
Guest User

Untitled

a guest
May 8th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.72 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. exec guile $0
  3. # !#
  4.  
  5. (import (ice-9 rdelim)
  6.         (ice-9 hash-table))
  7.  
  8. (define (count-or-unicode>? a b)
  9.   (or (> (car a) (car b))
  10.       (and (= (car a) (car b))
  11.            (string<? (cdr a) (cdr b)))))
  12.  
  13. (define (count-words port)
  14.   (define count (make-hash-table))
  15.   (let loop ([next (read-delimited " \t\n" port)])
  16.     (unless (eof-object? next)
  17.       (let ((num (hash-ref count next 0)))
  18.         (hash-set! count next (+ num 1))
  19.         (loop (read-delimited " \t\n" port)))))
  20.   (sort
  21.    (hash-fold (lambda (key val prior) (cons (cons val key) prior)) '() count)
  22.    count-or-unicode>?))
  23.  
  24. (for-each
  25.  (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x)))
  26.  (count-words (current-input-port)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement