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

Andrew Coppin andrewcoppin at btinternet.com
Tue Jan 13 13:13:20 EST 2009


Robin Green wrote:
> On Mon, 12 Jan 2009 21:04:35 +0100 (CET)
> Henning Thielemann <lemming at henning-thielemann.de> wrote:
>
>   
>> On Mon, 12 Jan 2009, Andrew Coppin wrote:
>>
>>     
>>> 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))
>>     
>
> I would use the more general idiom:
>
>      unfoldr (\n -> guard (n > 0) >> return (n `mod` b, n `div` b)

One of the wonderful things about Haskell is that almost any time 
anybody posts code, at least one person will think up an alternative but 
equivilent way of achieving the same goal - sometimes by radically 
different steps.

Maybe we should have a name for this effect?



More information about the Haskell-Cafe mailing list