使用来自histo的组织形式( http://hackage.haskell.org/package/recursion-schemes-5.0.3/docs/Data-Functor-Foldable.html ),我可以从初始列表中获得只包含奇数索引的a列表:
import Data.Functor.Foldable
odds :: [a] -> [a]
odds = histo $ \case
Nil -> []
Cons h (_ :< Nil) -> [h]
Cons h (_ :< Cons _ (t :< _)) -> h:t如何使用mhisto获得相同的东西?
nil = Fix Nil
cons a b = Fix $ Cons a b
list = cons 1 $ cons 2 $ cons 3 $ nil
modds :: Fix (ListF a) -> [a]
modds = mhisto alg where
alg _ _ Nil = []
alg f g (Cons a b) = ?发布于 2018-11-03 05:03:07
就是这样:
modds :: Fix (ListF a) -> [a]
modds = mhisto alg
where
alg _ _ Nil = []
alg odd pre (Cons a b) = a : case pre b of
Nil -> []
Cons _ b' -> odd b' GHCi> list = cata embed [1..10] :: Fix (ListF Int)
GHCi> odds (cata embed list)
[1,3,5,7,9]
GHCi> modds list
[1,3,5,7,9]odd折叠列表的其余部分,而pre则挖掘前一项。请注意,门德勒代数中的y -> f y函数的可用性反映了Cofree在普通组织形式代数中的引入(在这种代数中,可以通过触及Cofree流的尾部来进行回挖):
cata :: Functor f => (f c -> c) -> Fix f -> c
histo :: Functor f => (f (Cofree f c) -> c) -> Fix f -> c
mcata :: (forall y. (y -> c) -> f y -> c) -> Fix f -> c
mhisto :: (forall y. (y -> c) -> (y -> f y) -> f y -> c) -> Fix f -> c 有关mcata和mhisto的进一步阅读,请参阅, by Varmo Vene的第5章和第6章。
https://stackoverflow.com/questions/53126614
复制相似问题