I've written a function in Haskell for calculating the entropy of a collection. I'd like feedback on how the function could be rewritten to be more flexible/reusable, and also how to profile the function and how it could be tuned and/or modified for better performance.
import Data.List (foldl1') entropy :: [Int] -> Int -> Int -> Double entropy itemFrequencies totalElements logarithmicBase = -(foldl1' (+) $ map (\p -> p * (logBase b p)) probabilities) where is = map fromIntegral itemFrequencies l = fromIntegral totalElements b = fromIntegral logarithmicBase probabilities = map (\i -> i / l) $ is
For some background, the Entropy calculation is a core function used in building decision trees. For more complex datasets and decision trees, this function would get called very often. I'm working on a sequential implementation of the ID3 algorithm, which this entropy function is a part of, that I will later make parallel/concurrent as a separate exercise, and then I will eventually also write implementations of ID3's descendants: C4.5 and C5.0.
Please respect my desire to struggle on my own with the concurrency/parallelism aspect of rewriting this code, I'm only interested in any sequential improvements I could make for performance and any refactoring that could be done to make this code easier to maintain and reuse.