-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcount_chars.lisp
More file actions
28 lines (22 loc) · 853 Bytes
/
Copy pathcount_chars.lisp
File metadata and controls
28 lines (22 loc) · 853 Bytes
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
27
28
(defglobal *counts* (make-hash-table :size 36))
(defun count-chars (word)
"Counts each different alphabetical character in the given word"
(loop for c across word
when (alpha-char-p c) do
(incf (gethash (char-downcase c) *counts* 0))))
(defun count-chars-in-file (file)
(with-open-file (in file)
(loop for word = (read-line in nil) while word do
(count-chars word))))
(defun print-counts ()
"Print the counts for each char"
(loop for c from (char-code #\a) to (char-code #\z) do
(let* ((ch (code-char c))
(count (gethash (char-downcase (code-char c)) *counts* 0)))
(format t "~a: ~a~%" ch count))))
(let ((args (rest sb-ext:*posix-argv*)))
(if (= 1 (length args))
(progn
(count-chars-in-file (first args))
(print-counts))
(error "expected one argument")))