Created
December 3, 2013 00:54
-
-
Save chiku-samugari/7762038 to your computer and use it in GitHub Desktop.
Live coding session at Lisp Meet Up #11.
Make a frequency table and a histogram for a given list of floating point numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Utility | |
(defun iota (num &key (start 0) (step 1)) | |
(do ((i start (+ i step)) | |
(acc '() (cons i acc)) | |
(c 0 (1+ c))) | |
((= c num) (nreverse acc)))) | |
(defparameter *floats* | |
(mapcar (lambda (x) | |
(declare (ignore x)) | |
(float (/ (random 1000) 1000))) | |
(iota 1000))) | |
(let ((interval 0.05) | |
(table (make-hash-table))) | |
(dolist (elem *floats*) | |
(incf (gethash (floor elem interval) table 0))) | |
(let ((lst ())) | |
(maphash (lambda (k v) | |
(push (list (* k interval) (map 'string (constantly #\*) (iota v))) lst)) | |
table) | |
(format t "~{~&~{~6,2F : ~A~}~}" | |
(sort lst #'< :key #'car)))) | |
;;; Another implementation | |
(let ((interval 0.05) | |
(table (make-hash-table))) | |
(dolist (elem *floats*) | |
(incf (gethash (floor elem interval) table 0))) | |
(let ((lst ())) | |
(maphash (lambda (k v) | |
(push (list (* k interval) (make-list v :initial-element #\*)) lst)) | |
table) | |
(format t "~{~&~{~6,2F : ~{~A~}~}~}" | |
(sort lst #'< :key #'car)))) | |
;;; Yet another one | |
(let ((interval 0.05) | |
(table (make-hash-table))) | |
(dolist (elem *floats*) | |
(incf (gethash (floor elem interval) table 0))) | |
(let ((lst ())) | |
(maphash (lambda (k v) | |
(push (list (* k interval) (iota v)) lst)) | |
table) | |
(format t "~{~&~{~6,2F : ~{*~*~}~}~}" | |
(sort lst #'< :key #'car)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(make-string v :initial-element #\*)
なんて方法もあるみたいです