组织形态和门德勒

问题描述 投票:3回答:1

使用来自histo的histomorphism(recursion-schemes),我可以得到一个只包含初始列表中奇数索引的列表:

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) = ?
haskell recursion-schemes
1个回答
3
投票

就是这个:

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挖掘前任。请注意Mendler代数中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 

有关mcatamhisto的进一步阅读,请参阅Categorical programming with inductive and coinductive types, by Varmo Vene的第5章和第6章。

© www.soinside.com 2019 - 2024. All rights reserved.