[Haskell-cafe] Style
Henning Thielemann
lemming at henning-thielemann.de
Fri Aug 24 06:26:26 EDT 2007
On Fri, 24 Aug 2007, Mirko Rahn wrote:
>> tm = sum . takeWhile(>0) . iterate f . f
>> where f = flip div 5
>
> Quite nice. I like
>
> tm5 0 = 0
> tm5 n = let q = div n 5 in q + tm5 q
>
> This version corresponds to what I'm think when parsing |tm|, so I wrote it
> down directly.
Since the original poster used the message title "style", I want to
mention that his solution is good style, because it keeps logical steps
separated, and thus lets you inspect meaningful interim results.
(iterate f . f) computes the number of natural numbers up to n with
factors 5, 5^2, 5^3 and so on. Then you like to sum these up (sum), but
before this, you must limit the list to a reasonable finite prefix
(takeWhile (>0)). Of course, using 'show' and counting the zeros is more
intuitive. It is not efficient, but it will serve as a nice test of 'tm's
correctness.
More information about the Haskell-Cafe
mailing list