forked from benhoyt/countwords
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.lisp
27 lines (22 loc) · 1004 Bytes
/
simple.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
;; modified from http://rosettacode.org/wiki/Tokenize_a_string#Common_Lisp
(defun split-string (string)
(loop for start = 0 then (1+ finish)
for finish = (position #\Space string :start start)
collecting (subseq string start finish)
until (null finish)))
(defparameter *counter* (make-hash-table :test #'equal))
(defun trim-spaces (string)
(string-trim '(#\Space #\Tab #\Newline) string))
(defun update-word (word)
(incf (gethash word *counter* 0)))
(defun main ()
(loop for line = (read-line nil nil) while line
for words = (split-string (string-downcase (trim-spaces line)))
do (loop for word in words unless (zerop (length word))
do (update-word word)))
(let ((ordered (loop for key being the hash-keys of *counter*
using (hash-value value)
collect (cons key value))))
(sort ordered #'> :key #'cdr)
(dolist (pair ordered)
(format t "~a ~a~%" (car pair) (cdr pair)))))