Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 求无穷流的函子_Haskell_Functor_Recursive Datastructures - Fatal编程技术网

Haskell 求无穷流的函子

Haskell 求无穷流的函子,haskell,functor,recursive-datastructures,Haskell,Functor,Recursive Datastructures,我关注的是F-代数 它解释说 终端coalgebra在编程中通常被解释为一个配方 用于生成(可能无限)数据结构或转换 系统 他说 余代数的一个典型例子是基于一个函子,其 point是e类型元素的无限流。这是 函子: 这是它的固定点: data Stream e = Stream e (Stream e) 我试过这个密码 有关部分 newtype Fix f = Fix (f (Fix f)) unFix :: Fix f -> f (Fix f) unFix (Fix x) = x c

我关注的是F-代数 它解释说

终端coalgebra在编程中通常被解释为一个配方 用于生成(可能无限)数据结构或转换 系统

他说

余代数的一个典型例子是基于一个函子,其 point是e类型元素的无限流。这是 函子:

这是它的固定点:

data Stream e = Stream e (Stream e)
我试过这个密码

有关部分

newtype Fix f = Fix (f (Fix f))
unFix :: Fix f -> f (Fix f)
unFix (Fix x) = x

cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix

ana :: Functor f => (a -> f a) -> a -> Fix f
ana coalg = Fix . fmap (ana coalg) . coalg

data StreamF e a = StreamF e a
    deriving Functor
data Stream e = Stream e (Stream e)

era :: [Int] -> StreamF Int [Int]
era (p : ns) = StreamF p (filter (notdiv p) ns)
    where notdiv p n = n `mod` p /= 0

primes = ana era [2..]
我犯了这个错误

main.hs:42:14: error:
• Can’t make a derived instance of ‘Functor (StreamF e)’:
You need DeriveFunctor to derive an instance for this class
• In the data declaration for ‘StreamF’

我错在哪里了?

在Haskell中,如果不使用语言扩展,派生
非常有限。由于编译器不能始终计算出
函子
实例应该是什么,因此派生函子不是标准的Haskell

但是,有一个语言扩展允许这样做,即
-XDeriveFunctor
。要启用此扩展,请执行以下操作之一:

  • 使用标志
    -XDeriveFunctor
    编译。(例如:编译时运行
    ghc-XDeriveFunctor Main.hs

  • 在文件顶部编写pragma
    {-#LANGUAGE DeriveFunctor#-}

以下是添加此杂注后文件的外观:

{-# LANGUAGE DeriveFunctor #-}

newtype Fix f = Fix (f (Fix f))
unFix :: Fix f -> f (Fix f)
unFix (Fix x) = x

cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix

ana :: Functor f => (a -> f a) -> a -> Fix f
ana coalg = Fix . fmap (ana coalg) . coalg

data StreamF e a = StreamF e a
    deriving Functor
data Stream e = Stream e (Stream e)

era :: [Int] -> StreamF Int [Int]
era (p : ns) = StreamF p (filter (notdiv p) ns)
    where notdiv p n = n `mod` p /= 0

primes = ana era [2..]
如果您计划使用GHCi,请在加载文件之前使用
:set-XDeriveFunctor

在vanilla Haskell中不能使用
派生(Functor)
。您需要在文件顶部使用pragma
{-#LANGUAGE DeriveFunctor#-}
。更多信息。相反,编译器可以派生一个
函子
实例(这样做很简单),在Haskell 2010中不需要这样做。
{-# LANGUAGE DeriveFunctor #-}

newtype Fix f = Fix (f (Fix f))
unFix :: Fix f -> f (Fix f)
unFix (Fix x) = x

cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix

ana :: Functor f => (a -> f a) -> a -> Fix f
ana coalg = Fix . fmap (ana coalg) . coalg

data StreamF e a = StreamF e a
    deriving Functor
data Stream e = Stream e (Stream e)

era :: [Int] -> StreamF Int [Int]
era (p : ns) = StreamF p (filter (notdiv p) ns)
    where notdiv p n = n `mod` p /= 0

primes = ana era [2..]