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

11 lines
391 B
Common Lisp

(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))
(maphash (lambda (k v)
(decf entropy (* (/ v (length input-string))
(log (/ v (length input-string)) 2))))
table)
entropy))