[Haskell-cafe] unfoldr with a little extra

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Sun Aug 3 02:11:54 UTC 2014


David Feuer wrote:
> unfoldr1::(b -> (a, Maybe b)) -> b -> [a]
> unfoldr1 f b= go b
>   where
>     go q = case f q of
>       (a,may_b) -> a : maybe [] go may_b
> 
> My question is whether it is possible to write this function using the
> usual unfoldr. I have the feeling there might be some way, but I just can't
> see it.

We need some common ground between f :: b -> (a, Maybe b) and the type
of the first argument of unfoldr, b -> Maybe (a, b); the best I can
see is (fmap f) :: Maybe b -> Maybe (a, Maybe b). So

  unfoldr1 f b = unfoldr (fmap f) (Just b)

Cheers,

Bertram


More information about the Haskell-Cafe mailing list