![](/img/trans.png)
[英]GHC 7.10.2 warning when deriving Unbox instance using Data.Vector.Unboxed.Deriving
[英]Haskell: How To Fix Error With “add An Instance Declaration For (Unbox A)” When Using Unboxed Vectors?
我編寫了一些代碼,其中一小部分代碼采用大型一維Unboxed.Vector並將它們作為Vector(Vector a)返回。
代碼的一部分是出錯。 下面是一段與實際代碼類似的示例代碼,並給出了相同的錯誤。
import Data.Vector.Unboxed as D
xs = [0,1,2,3,4,5,6,7,8,9,10,11]
rows = 3
cols = 4
sb = D.fromList xs
takeRows::Int -> Int -> Vector Int -> Vector (Vector Int)
takeRows rows cols x0 = D.map (\x -> D.slice x (fromIntegral cols) x0) starts
where
starts = D.enumFromStepN 0 cols rows
-- takeRowsList::Int -> Int -> Vector Int -> [Vector Int]
-- takeRowsList rows cols x0 = Prelude.map (\x -> D.slice x (fromIntegral cols) x0) starts
-- where
-- starts = D.toList . D.enumFromStepN 0 cols $ rows
錯誤是
No instance for (Unbox (Vector Int))
arising from a use of `D.map'
Possible fix: add an instance declaration for (Unbox (Vector Int))
In the expression:
D.map (\ x -> slice x (fromIntegral cols) x0) starts
In an equation for `takeRows':
takeRows rows cols x0
= D.map (\ x -> slice x (fromIntegral cols) x0) starts
where
starts = enumFromStepN 0 cols rows
我已經寫了一個類似的函數takeRowsList,它使外部Vector作為List,這不會遇到同樣的問題。 我也把它包含在上面但是評論出來,以證明我的問題。
我知道有些函數需要類型定義,當我將它們與Unboxed Vectors一起使用時。 但在這種情況下,我很難在哪里放置類型定義。 我已經嘗試了很多類型定義一切,我不斷收到上述錯誤。
在此先感謝您的幫助。
未裝箱的向量需要知道其元素的大小,並且該大小必須是常量。 向量可以具有不同的大小,因此它們不能是未裝箱的向量的元素。 但它們可能是盒裝向量的元素,因此如果列表不適合您的操作,可以將其設置為盒裝向量( import qualified Data.Vector as B
並使用B而不是D限定相關函數)。
您不能讓未裝箱的矢量包含另一個矢量。 只能將某些原始數據類型取消裝箱,即為其定義Unbox
實例的數據類型。 向量不是原始數據類型。
你可以做的是讓你的函數返回一個未裝箱的矢量的普通(盒裝)矢量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.