Advertisement
Guest User

Untitled

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