(module countwords () (import scheme) (import (chicken base) (chicken plist) (chicken sort)) (define (count-words port) (let ((word-count '())) (let loop ((word '())) (let ((char (read-char port))) (if (eof-object? char) (for-each (lambda (sym) (print sym " " (get sym 'count))) (sort word-count (lambda (a b) (> (get a 'count) (get b 'count))))) (cond ((or (char=? char #\space) (char=? char #\newline)) (unless (null? word) (let* ((sym (string->symbol (list->string (reverse word)))) (count (get sym 'count 0))) (put! sym 'count (add1 count)) (when (zero? count) (set! word-count (cons sym word-count))))) (loop '())) (else (loop (cons (char-downcase char) word))))))))) (count-words (current-input-port)) ) ;; end module