Matrix和Vector构造函数都有友好的*->*,所以它们看起来像值构造函数。但是当我尝试像这样的东西
instance Functor Vector a where
fmap g ( Vector a ) = Vector ( g a )我得到了这个错误:
Not in scope: data constructor `Vector'这是有道理的,因为我无论如何都不能使用let v = Vector [1..3]来生成一个向量。但是检查源代码,我发现Matrix和Vector构造函数都是从各自的模块导出的:
Vector.hs
module Data.Packed.Vector (
Vector,
fromList, (|>), toList, buildVecto..
) where
Matrix.hs
module Data.Packed.Matrix (
Element,
Matrix,rows,cols...
) whereDido用于应用函数器、单元体等。
发布于 2013-03-08 01:58:20
module Data.Packed.Vector (
Vector,
fromList, (|>), toList, buildVecto..
) where这会公开类型Vector,但不会公开它的任何构造函数。
您的实例声明已更正:
instance Functor Vector where
fmap = V.map(假设您使用import Vector as V,并进一步假设您谈论的是向量包中的向量)。
EDIT:对不起,没看到你提到包的名字。对于hmatrix向量,它将是mapVector而不是V.map。
EDIT_ 2:正如其他人所提到的,对于hmatrix,这不会起作用,因为矩阵和向量的内容都需要Storeable。
发布于 2013-03-08 16:28:26
正如Conrad Parker所说,我们需要Storable实例。
{-# LANGUAGE ConstraintKinds, TypeFamilies #-}
import Numeric.LinearAlgebra
import Foreign.Storable(Storable)
import GHC.Exts (Constraint)
class Functor' c where
type Ok c u v :: Constraint
type Ok c u v = ()
fmap' :: Ok c u v => (u -> v) -> c u -> c v
instance Functor' Vector where
type Ok Vector u v = (Storable u, Storable v)
fmap' = mapVectorhttps://stackoverflow.com/questions/15278221
复制相似问题