gpt4 book ai didi

arrays - 如何嵌套未装箱的向量?

转载 作者:行者123 更新时间:2023-12-03 21:47:11 25 4
gpt4 key购买 nike

这可能是一个非常基本的问题,但我四处搜索,似乎找不到答案。

我想使用未装箱的向量表示一个 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 中,这也需要一个 "jagged" matrix ,通常用指针表示(=装箱)。

关于arrays - 如何嵌套未装箱的向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44082787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com