Skip to content

Instantly share code, notes, and snippets.

@chiku-samugari
Created December 3, 2013 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiku-samugari/7762038 to your computer and use it in GitHub Desktop.
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.
;;; 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))))
@KeenS
Copy link

KeenS commented Dec 5, 2013

(make-string v :initial-element #\*)なんて方法もあるみたいです

@chiku-samugari
Copy link
Author

気が付きませんでしたが、そのやり方が簡潔ですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment