[Haskell-cafe] unfoldr [ANN: HLint 1.2]

Yitzchak Gale gale at sefer.org
Tue Jan 13 08:45:02 EST 2009


Andrew Coppin wrote:
>>> Does it suggest unfoldr too?

I think Neil's idea to have this customizable is a good one.
It's often a matter of taste.

I would rarely want to use unfoldr, and I wouldn't want HList
to bother me about it. Instead, I prefer to use iterate for both
of Andrew's examples:

>  convert b 0 = []
>  convert b n = n `mod` b : convert b (n `div` b)
>
>  convert b = unfoldr (\n -> if n > 0 then Just (n `mod` b, n `div` b) else
> Nothing)

convert b = map (`mod` b) . takeWhile (> 0) . iterate (`div` b)

>  heap_to_list = unfoldr (\h -> if heap_empty h then Nothing else Just
> (heap_top h, heap_delete_top h))

heap_to_list = map heap_top . takeWhile (not . heap_empty) .
   iterate heap_delete_top

Here is one case where I actually do use unfoldr:

-- Mine free-form user input for occurrences of a data type
readMany = unfoldr $ listToMaybe . concatMap reads . tails

ghci> readMany "The numbers are 3, 7, and 42." :: [Int]
[3,7,42]

But I don't believe HLint should be expected to come up with
something like that. It's quite rare.

Regards,
Yitz


More information about the Haskell-Cafe mailing list