RosettaCodeData/Task/Entropy/Common-Lisp/entropy-1.lisp

11 lines
391 B
Common Lisp
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
(defun entropy (string)
(let ((table (make-hash-table :test 'equal))
(entropy 0))
(mapc (lambda (c) (setf (gethash c table) (+ (gethash c table 0) 1)))
(coerce string 'list))
2018-06-22 20:57:24 +00:00
(maphash (lambda (k v)
(decf entropy (* (/ v (length input-string))
(log (/ v (length input-string)) 2))))
2015-11-18 06:14:39 +00:00
table)
entropy))