The example given in the documentation of unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
:
unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
can easily be written with a redundant pair:
unfoldr (\b -> if b == 1 then Nothing else Just (b-1, b-1)) 11
What does unfoldr
need the pair (a,b)
for? Why is its type not (a -> Maybe a) -> a -> [a]
?
A function with type
(a -> Maybe a) -> a -> [a]
restricts the output list element type to be the same as the state which is threaded through the generation process. unfoldr
is more general in that it allows an independent type of state to be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With