Entropy/Narcissist: Difference between revisions

Content added Content deleted
m (added a clarifying word to the task's requirement.)
(→‎{{header|Haskell}}: Changed to binary analyser)
Line 255: Line 255:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List
<lang haskell>import qualified Data.ByteString as BS
import Data.List
import System.Environment


(>>>) = flip (.)
main = readFile "Narcissist.hs" >>= print . entropy
where entropy = sum . map lg . fq . map genericLength . group . sort
lg c = -c * logBase 2 c
fq c = let sc = sum c in map (/ sc) c</lang>


main = getArgs >>= head >>> BS.readFile >>= BS.unpack >>> entropy >>> print
{{out}}
entropy = sort >>> group >>> map genericLength >>> frequency >>> map lg >>> sum
where lg c = -c * logBase 2 c
frequency c = let sc = sum c in map (/ sc) c</lang>


{{out}} In a shell
4.25305115699825

$ ghc --make -O3 Narcissist.hs

Entropy of the source

$ ./Narcissist Narcissist.hs
4.444689880591661

Entropy of the binary

$ ./Narcissist Narcissist
5.525417236346172


=={{header|J}}==
=={{header|J}}==