计划中的向量
Vector in scheme
来自 Racket 中 equals?
的文档:
Equal? recursively compares the contents of pairs, vectors, and strings, applying eqv? on other objects such as numbers and symbols. A rule of thumb is that objects are generally equal? if they print the same. Equal? may fail to terminate if its arguments are circular data structures.
(equal? 'a 'a) ===> #t`
(equal? '(a) '(a)) ===> #t`
scheme中的vector到底是什么?例如,(1. 2)
是向量吗? (1 2)
是向量吗? (1 2 3 4)
是向量吗?等等
文档列出了 vector 和 vector?
等,但我想知道他们是否只是使用 vector 来表示“列表”或其他含义:https://people.csail.mit.edu/jaffer/r5rs/Disjointness-of-types.html#Disjointness-of-types
向量就是 vector
。它是一种不同的数据结构,类似于我们在其他编程语言中通常所说的“数组”。它有两种风格,可变的和不可变的——例如:
(vector 1 2 3) ; creates a mutable vector via a procedure call
=> '#(1 2 3)
'#(1 2 3) ; an immutable vector literal
=> '#(1 2 3)
(vector-length '#(1 2 3))
=> 3
(vector-ref '#(1 2 3) 1)
=> 2
(define vec (vector 1 2 3))
(vector-set! vec 1 'x)
vec
=> '#(1 x 3)
您可能会问自己,与良好的旧列表相比,矢量的优势是什么。好吧,可变向量可以就地修改,访问给定索引的元素是一个常量 O(1) 操作,而在列表中,相同的操作是 O(n)。同样对于长度操作。缺点是创建后不能增加矢量的大小(不像 Python 列表);如果您需要添加更多元素,则必须创建一个新向量。
有许多特定于向量的操作反映了可用于列表的过程;我们有 vector-map
、vector-filter
等等,但我们也有 vector-map!
之类的东西,可以就地修改矢量,而不创建新矢量。查看documentation了解更多详情!
来自 Racket 中 equals?
的文档:
Equal? recursively compares the contents of pairs, vectors, and strings, applying eqv? on other objects such as numbers and symbols. A rule of thumb is that objects are generally equal? if they print the same. Equal? may fail to terminate if its arguments are circular data structures.
(equal? 'a 'a) ===> #t` (equal? '(a) '(a)) ===> #t`
scheme中的vector到底是什么?例如,(1. 2)
是向量吗? (1 2)
是向量吗? (1 2 3 4)
是向量吗?等等
文档列出了 vector 和 vector?
等,但我想知道他们是否只是使用 vector 来表示“列表”或其他含义:https://people.csail.mit.edu/jaffer/r5rs/Disjointness-of-types.html#Disjointness-of-types
向量就是 vector
。它是一种不同的数据结构,类似于我们在其他编程语言中通常所说的“数组”。它有两种风格,可变的和不可变的——例如:
(vector 1 2 3) ; creates a mutable vector via a procedure call
=> '#(1 2 3)
'#(1 2 3) ; an immutable vector literal
=> '#(1 2 3)
(vector-length '#(1 2 3))
=> 3
(vector-ref '#(1 2 3) 1)
=> 2
(define vec (vector 1 2 3))
(vector-set! vec 1 'x)
vec
=> '#(1 x 3)
您可能会问自己,与良好的旧列表相比,矢量的优势是什么。好吧,可变向量可以就地修改,访问给定索引的元素是一个常量 O(1) 操作,而在列表中,相同的操作是 O(n)。同样对于长度操作。缺点是创建后不能增加矢量的大小(不像 Python 列表);如果您需要添加更多元素,则必须创建一个新向量。
有许多特定于向量的操作反映了可用于列表的过程;我们有 vector-map
、vector-filter
等等,但我们也有 vector-map!
之类的东西,可以就地修改矢量,而不创建新矢量。查看documentation了解更多详情!