问题描述
我以前读过这个问题,并在.
I've read this question before, and followed Eli Barzilay's answer on srfi-25.
除了阅读srfi-25的源代码,我发现写一些辅助函数会容易得多,例如
Besides reading the source code of srfi-25, I found writing some auxiliary function would be much more easier, for example
#lang racket
(define (set2v! vec x y value)
(vector-set! (vector-ref vec x) y value))
(define (get2v vec x y)
(vector-ref (vector-ref vec x) y))
(define v2 (vector (vector 1 2 3) (vector 4 5 6) (vector 7 8 9)))
(get2v v2 1 1)
(set2v! v2 1 1 99)
(get2v v2 1 1)
我想知道在多维向量操作上是否有一些Racket-y方式?
I was wondering if there maybe some Racket-y way on multidimensional vectors operation?
推荐答案
对多维向量使用嵌套向量的替代方法是使用数学库的 array 数据结构.
An alternative to using nested vectors for multidimensional vectors is to use the math library's array data structure.
这是一个使用示例:
Welcome to Racket v6.4.0.4.
-> (require math/array)
-> (define arr (mutable-array #[#[1 2 3] #[4 5 6]]))
-> (array-ref arr #(0 0))
1
-> (array-ref arr #(1 2))
6
-> (array-set! arr #(1 2) 15)
-> (array-ref arr #(1 2))
15
需要注意的是,当您从无类型代码(例如,#lang racket
)使用库时,这会变慢.在 Typed Racket 中使用它会很快.
There is a caveat that this will be slower when you use the library from untyped code (e.g., #lang racket
). It will be fast when used in Typed Racket.
这篇关于多维向量操作的 Racket-y 方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!