haskell - 在 Haskell 中创建一个可变的 Data.Vector

标签 haskell vector mutable

我希望使用 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/

相关文章:

haskell - 如何与类型 "Either a b"匹配?

c++ - 关于二维 vector 的一些问题,C++

R:粘贴向量的元素

javascript - 如何在一个简单的数组上实现空间 trim

debugging - 让 "trace"像 "assert"一样优化?

list - 理解递归定义的列表(用 zipWith 表示的 fibs)

haskell - 具有惰性评估和内存消耗的蛮力

c++ - vector resize() 自动填充

Java HashMap/Hashtable 不检索自己的键

rust - 函数调用后使用rust 的可变性要求