Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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_Vector_Printing_Mutable - Fatal编程技术网

Haskell如何打印可变向量

Haskell如何打印可变向量,haskell,vector,printing,mutable,Haskell,Vector,Printing,Mutable,我想打印MVector(很惊讶没有show实例)。所以我必须先把它放到向量上。 然后我得到了类型错误: import Control.Monad.IO.Class (liftIO) import Control.Monad.Primitive import qualified Data.Vector as V import qualified Data.Vector.Mutable as MV fromList :: [

我想打印
MVector
(很惊讶没有show实例)。所以我必须先把它放到向量上。 然后我得到了类型错误:

import           Control.Monad.IO.Class  (liftIO)
import           Control.Monad.Primitive
import qualified Data.Vector             as V
import qualified Data.Vector.Mutable     as MV

fromList :: [a] -> IO (MV.IOVector a)
fromList = V.thaw . V.fromList

printMV :: PrimMonad m => MV.MVector (PrimState m) a -> m ()
printMV = liftIO . print . V.freeze
我还尝试了
IOVector

Algo/QuickSort.hs:12:11: error: …
    • Couldn't match type ‘PrimState m’ with ‘PrimState m0’
      Expected type: MV.MVector (PrimState m) a -> m ()
        Actual type: MV.MVector (PrimState m0) a -> m ()
      NB: ‘PrimState’ is a non-injective type family
      The type variable ‘m0’ is ambiguous
    • In the expression: liftIO . print . V.freeze
      In an equation for ‘printMV’: printMV = liftIO . print . V.freeze
    • Relevant bindings include
        printMV :: MV.MVector (PrimState m) a -> m ()
          (bound at /home/skell/btree/Algo/QuickSort.hs:12:1)
   |
Compilation failed.
这次错误不同:

printMV :: MV.IOVector a -> IO ()
printMV = liftIO . print . V.freeze

有两件事正在进行-第一个解决方案:

printMV::MonadIO m=>Show a=>PrimMonad m=>MV.MVector(PrimState m)a->m()
printMV v=do
fv MV.IOVector a->IO()
printMV2 v=v.freeze v>>=print
  • 需要
    a的
    Show
    实例
  • 需要
    >=
    冻结
    的结果(与上面相同-
    do
    隐式执行此操作)
  • 这里不需要
    liftIO
    ,因为您已经在其中了

不能有
Show
实例(或者至少不能是您可能想到的
Show
实例),因为
Show
不允许执行
IO
,而
IO
是读取向量所必需的。@DanielWagner那么你的意思是所有Haskeller都像我刚才那样打印
vector
?嗯,这种说法可能太强烈了<代码>冻结
ing绝对是一种方式;但是还有很多其他的。例如,可以在不冻结的情况下迭代索引,或者以更易于人类阅读的特定于算法的顺序呈现元素,或者将向量转换为树,因为它实际上是一个线性化的堆,或者使用向量创建一个单元计算的小部件,或者,或者…@DanielWagner你如何迭代一个
MVector
?我找不到它的
可折叠
可遍历
实例。我能以某种方式
mapM\uuuuu打印它吗?不可能有
可折叠的
可遍历的
实例,原因与不可能有
显示
实例基本相同。但是,例如,
mapM(MV.read v>=>print)[0..MV.length v-1]
对于基于
IO
的向量非常有效,其中
MV
Data.Vector.Mutable
。(如果您觉得需要速度,也可以使用
MV.unsafeRead
。如果高级Haskeller不信任列表融合来消除
[0..MV.length v-1],也可以手动编写循环。)
。但这两种情况只有在验证此循环的速度是否真正重要后才会发生——打印时几乎从来不会发生。)
Algo/QuickSort.hs:12:28: error: …
    • Couldn't match type ‘PrimState m0’ with ‘RealWorld’
      Expected type: MV.IOVector a -> m0 (V.Vector a)
        Actual type: MV.MVector (PrimState m0) a -> m0 (V.Vector a)
      The type variable ‘m0’ is ambiguous
    • In the second argument of ‘(.)’, namely ‘V.freeze’
      In the second argument of ‘(.)’, namely ‘print . V.freeze’
      In the expression: liftIO . print . V.freeze
   |
Compilation failed.