创建CArray的CArray

时间:2018-10-16 10:08:01

标签: arrays haskell

我想创建一个CArray的CArray:

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}

module Main where

import Data.Array.CArray
import Data.Array.Base

testArr :: CArray Int Double
testArr =  listArray (0,5) [0..5]

-- divvyArray :: Int -> CArray Int Double -> CArray Int (CArray Int Double)
divArray w arr = arr'
  where (m1,m2) = bounds arr
        len = m2 - w + 2
        divInd = zip [0..(m2-w+1)] [w..m2]
        f ix = slice (0,w) ix arr 
        arr' = listArray (0,len) [ f k | k <- divInd] 

代码会编译并具有类型divArray :: (Enum p, Shapable p, IArray a1 e, IArray a2 (a1 p e), Ix p, Num p) => p -> a1 p e -> a2 p (a1 p e)'

但是当我运行它

  

divArray 3 testArray

出现此错误:

<interactive>:168:1: error:
    • Non type-variable argument
        in the constraint: IArray a2 (CArray Int Double)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall (a2 :: * -> * -> *).
              IArray a2 (CArray Int Double) =>
              a2 Int (CArray Int Double)

[]中给出一个想法,结果应该是:

  

divList 3 [0..5]

[[0..2],[1..3],[2..4],[3..5]]

1 个答案:

答案 0 :(得分:1)

您必须提供divArray函数的返回类型为:

divArray 3 testArr::CArray Int (CArray Int Double)

否则,没有Foreign.Storable.Storable类类型的默认CArray实例。如果没有这种情况,则会出现以下错误

  

(Foreign.Storable.Storable(CArray Int Double))没有实例

只有Storable的实例可以是CArray的元素。