word counting added by chickendan on Fri Apr 12 22:03:06 2019

;; word count problem statement (https://ptrace.fefe.de/wp/README.txt):
;; - read stdin, tokenize into words
;; - for each word count how often it occurs
;; - output words and counts, sorted in descending order by count

;; input:
;; wc -w words.txt: 41,616,100 (217 MB)
;; tr " " "\n" < words.txt | sort | uniq | wc -l: 557,732

;; timings:
;; chicken:    144s (csc -O5 -strict-types -fixnum-arithmetic -o chicken wp.scm)
;; python:     32s
;; ruby:       70s
;; lua/luajit: 36s/25s
;; perl:       33s

;; profiling via -:p (O4), 3m32s:
;; procedure                                     calls  seconds  average  percent
;; ------------------------------------------------------------------------------
;; wp.scm:8: srfi-69#hash-table-update!/default   3934  122.689    0.031   61.600
;; wp.scm:9: chicken.string#string-split          3866   63.000    0.016   31.631
;; wp.scm:13: chicken.base#print                     1    7.160    7.160    3.594
;; wp.scm:7: g28                                   288    3.100    0.010    1.556
;; wp.scm:14: chicken.sort#sort                      1    2.560    2.560    1.285
;; wp.scm:14: srfi-69#hash-table-map                 1    0.660    0.660    0.331

(import (chicken port) (chicken io) (chicken string) (chicken sort) (chicken fixnum) srfi-69)

(define-inline (1+ x) (fx+ 1 x))

(let ((words (make-hash-table string=? string-hash)))
  (port-for-each (lambda (line)
                   (for-each (lambda (word)
                               (hash-table-update!/default words word 1+ 0))
                             (string-split line)))
                 read-line)

  (for-each (lambda (pair)
              (print (cdr pair) " " (car pair)))
            (sort (hash-table-map words (lambda (k v) (cons k v)))
                  (lambda (a b) (fx< (cdr a) (cdr b))))))