You've almost got it! The original function uses the function p
(for 'predicate') to determine whether we've finished unfolding, h
to apply to each element, and t
(for 'transformation') to transform an element into the seed for the rest of the list.
unfoldr
expects a single function f :: b -> Maybe (a,b)
, which returns Nothing
if we've finished unfolding, or Just (x, y)
, where x
is the element to add to the list and y
is the seed for the rest of the list.
So f
in unfoldr
is responsible for the functionality of all three of p
, h
and t
. The Nothing
-or-Just
dichotomy plays the part of the Boolean function p
, and the second element of the tuple does t
's job of supplying the seed for the rest of the list.
Here's my solution (I've renamed the variables from your question, for clarity):
listUnfold pred f trans seed =
unfoldr (\x -> if pred x
then Nothing
else Just (f x, trans x)) seed
Of course, when a value appears on the right-hand-end of a definition, as seed
does here, you can take advantage of Haskell's sexy syntax for currying and throw it away altogether:
listUnfold pred f trans = unfoldr (\x -> if pred x
then Nothing
else Just (f x, trans x))
Formally, this transformation is known as eta reduction.