[Haskell-cafe] Style

sievers at math2.nat.tu-bs.de sievers at math2.nat.tu-bs.de
Fri Aug 24 09:50:40 EDT 2007


Arie Groeneveld wrote:

> tm = sum . takeWhile(>0) . iterate f . f
>    where f = flip div 5

> Which one is the most elegant one generally speaking?

I like that tm only uses div.

> My personal choice is 'tm'.
> I like 'tm3' (a revised version of tm2) in terms of
> pointlessness and not having a 'where', but I think
> it's a bit contrived because of the 'flip'.

You can make tm whereless by noticing that you use because
you use the function twice in  iterate f . f, which is because
you don't want the initial value that iterate gives.
You can instead use tail on iterate's result, and use a section
to avoid flip:

tm = sum . takeWhile (>0) . tail . iterate (`div` 5)

(Hope that works, can't test now...)


All the best
Christian Sievers


More information about the Haskell-Cafe mailing list