foldrとunfoldrは双対
並べてみるとわかりやすい
型の対応がキレイに取れている
foldr
code:hs
foldr :: (Maybe (a, b) -> b) -> a -> b foldr f [] = f Nothing
foldr f (x:xs) = f (Just (x, foldr f xs))
unfoldr
code:hs
unfoldr :: (a -> Maybe (b, a)) -> a -> b unfoldr f x = case f x of
Nothing -> []
Just (b, a) -> b : unfoldr f a