我正在阅读维基百科上有关“catamorphisms”(一种函数式编程概念)的文章,目前我已经能够在 F# 中复制 Haskell 的示例,但有一个部分我还没做到:
type Algebra f a = f a -> a -- the generic f-algebras
newtype Fix f = Iso { invIso :: f (Fix f) } -- gives us the initial algebra for the functor f
cata :: Functor f => Algebra f a -> (Fix f -> a) -- catamorphism from Fix f to a
cata alg = alg . fmap (cata alg) . invIso -- note that invIso and alg map in opposite directions
在F#中是否可能做到这一点?
List.fold
)定义它,但不能像你引用的示例一样通用地定义它。 - Benjamin HodgsonT f = f Bool
,其中f : *->*
。你可以在 Haskell 或 Scala 中实现,但我不知道 F# 是否允许这样做。 - chi