I am learning recursion schemes and it has proven very helpful to me to implement them specific to the list type. However, I got stuck on apomorphisms.
Here is an implementation of tails
in terms of apo I recently found:
import Data.Functor.Foldable
tailsApo :: [a] -> [[a]]
tailsApo = apo coalgTails
where
coalgTails = \case
[] -> Cons [] (Left [])
li@(_:xs) -> Cons li (Right xs)
Unfortunately, I couldn't import Data.Functor.Foldable
with GHCi, because I get a package not found error. Another search revealed this implemenation of apo specific to lists:
apoList :: ([b] -> Maybe (a, Either [b] [a])) -> [b] -> [a]
apoList f b = case f b of
Nothing -> []
Just (x, Left c) -> x : apoL f c
Just (x, Right e) -> x : e
Obviously, apoList
's first argument doesn't match with tailsApo
. I'd expext the type to be something like apoList :: ([b] -> Either (a, b) [a])) -> [b] -> [a]
.
There seems to be no more beginner friendly information on this subject. I appreciate any help.