[Haskell-cafe] unfoldr [ANN: HLint 1.2]
Henning Thielemann
lemming at henning-thielemann.de
Mon Jan 12 15:04:35 EST 2009
On Mon, 12 Jan 2009, Andrew Coppin wrote:
> Off the top of my head, try this:
>
> convert b 0 = []
> convert b n = n `mod` b : convert b (n `div` b)
>
> (Takes a number and yields the radix-B representation of it. Backwards.)
>
> convert b = unfoldr (\n -> if n > 0 then Just (n `mod` b, n `div` b) else Nothing)
I have the nice function 'toMaybe' which simplifies this to:
unfoldr (\n -> toMaybe (n>0) (n `mod` b, n `div` b))
Maybe HLint can also suggest non-base functions? ;-)
http://hackage.haskell.org/packages/archive/utility-ht/0.0.2/doc/html/Data-Maybe-HT.html
More information about the Haskell-Cafe
mailing list