[英]How to nest unboxed vectors?
這可能是一個非常基本的問題,但我已經四處尋找,似乎無法找到答案。
我想使用未裝箱的矢量來表示2D列表。 使用法線向量很容易做到這一點:
> import qualified Data.Vector as V
> V.fromList [V.fromList [1..5]]
[[1,2,3,4,5]]
但是,如果我嘗試使用未裝箱的矢量:
> import qualified Data.Vector.Unboxed as U
> U.fromList [U.fromList [1..5]]
我收到以下錯誤:
• Non type-variable argument
in the constraint: U.Unbox (U.Vector a)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a.
(U.Unbox (U.Vector a), U.Unbox a, Num a, Enum a) =>
U.Vector (U.Vector a)
我懷疑它與此有關:
> V.fromList [1..5]
[1,2,3,4,5]
而
> U.fromList [1..5]
[1.0,2.0,3.0,4.0,5.0]
但我似乎無法理解如何避免這種情況。
提前致謝!
好吧,我們可以開始遵循編譯器給出的建議:
> :set -XFlexibleContexts
然而:
> U.fromList [U.fromList [1..5]]
<interactive>:10:1: error:
• No instance for (U.Unbox (U.Vector a0))
arising from a use of ‘print’
這里的問題是您無法取消裝箱矢量(即使它是未裝箱的數據)。 要取消裝箱數據類型,您需要精確描述字節布局,包括其大小。 但是矢量沒有大小。 考慮例如
U.fromList [U.fromList [1..5], U.fromList [1..7]]
即使在C中,這也需要一個“鋸齒狀”矩陣 ,通常用指針(=拳擊)表示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.