我希望使用 Data.Vector.Generic.Mutable.new 创建一个可变向量。我找到了通过解冻纯向量来创建可变向量的示例,但这不是我想要做的。
这是许多失败的尝试之一:
import Control.Monad.Primitive
import qualified Data.Vector.Generic.Mutable as GM
main = do
v <- (GM.new 10) :: (GM.MVector v a) => IO (v RealWorld a)
GM.write v 0 (3::Int)
x <- GM.read v 0
putStrLn $ show x
给我错误
No instance for (GM.MVector v0 Int)
arising from an expression type signature
Possible fix: add an instance declaration for (GM.MVector v0 Int)
我尝试了基于 Haskell Vector 教程的变体,但没有成功。
我也欢迎有关构建载体的更清洁方法的建议。对 RealWorld 的引用对我来说似乎很丑陋。
最佳答案
GM.MVector v a
约束在 v
中是不明确的.也就是说,根据你给GHC的类型信息,它仍然无法弄清楚GM.MVector
的具体实例是什么。你想要它使用。对于 Int
的可变向量使用 Data.Vector.Unboxed.Mutable
.
import qualified Data.Vector.Unboxed.Mutable as M
main = do
v <- M.new 10
M.write v 0 (3 :: Int)
x <- M.read v 0
print x
关于haskell - 在 Haskell 中创建一个可变的 Data.Vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739384/