word-count pasted by mario-goulart on Thu Mar 18 09:54:14 2021
(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
countwords with plists added by mario-goulart on Mon Mar 22 22:12:42 2021
(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