(module countwords () (import scheme) (import (chicken base)) (import srfi-69) (define (count-words port) (let ((word-count (make-hash-table test: string=?))) (let loop ((word '())) (let ((char (read-char port))) (if (eof-object? char) (hash-table-for-each word-count (lambda (word count) (print word " " count))) (cond ((or (char=? char #\space) (char=? char #\newline)) (hash-table-update!/default word-count (list->string (reverse word)) add1 0) (loop '())) (else (loop (cons (char-downcase char) word))))))))) (count-words (current-input-port)) ) ;; end module