Advertisement
Guest User

count-word-occurences.rkt

a guest
Mar 18th, 2021
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 0.86 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (increment-word hashmap words)
  4.   (if (empty? words)
  5.       hashmap
  6.       (let ([word (car words)])
  7.         (let ([count (hash-ref hashmap word 0)])
  8.           (let ([hashmap-next (hash-set hashmap word (+ count 1))])
  9.             (increment-word hashmap-next (cdr words)))))))
  10.  
  11. (define (process-line hashmap line)
  12.   (let ([words (string-split (string-downcase line))])
  13.     (increment-word hashmap words)))
  14.  
  15. (define (read-from-stdin)
  16.   (define (read-from-stdin-it hashmap)
  17.     (let ([line (read-line)])
  18.       (if (eof-object? line)
  19.           hashmap
  20.           (read-from-stdin-it (process-line hashmap line)))))
  21.   (let ([wc (hash)])
  22.     (read-from-stdin-it wc)))
  23.  
  24. (define sorted (sort (hash->list (read-from-stdin))
  25.                      >
  26.                      #:key cdr))
  27.  
  28. (for ([pair sorted])
  29.   (printf "~A ~A~%" (car pair) (cdr pair)))
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement