4 ms·
best define `average []` first
by DarylZero 5y ago
best define `average []` first
- chriswarbo 5y agoHeh; better to use a non-empty list (AKA a tuple) I suppose. It's also wildly inefficient compared to: average (x, xs) = total / count where (total, count) = Data.List.foldl' go (x, 1) xs go (total, count) x = (total + x, count + 1) In a naive interpreter, this will do one pass rather than two. A smart compiler like GHC can probably optimise away the entire list, getting a tight numeric loop (e.g. https://dl.acm.org/doi/10.1145/1291220.1291199 https://dl.acm.org/doi/10.1145/1291220.1291199 )