This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-133@nospamsrfi.schemers.org
. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.
vector-unfold!
)vector-cumulate
from (vector-cumulate f vec knil)
to
(vector-cumulate f knil vec)
to match the
argument order of vector-fold.)vector-partition
.)elt=?
argument be consistent with eq?
.)Post-finalization note: The finalized version of this
SRFI required the equality operator used by the
vector=
procedure to be consistent with
eq?
. However, =
is not consistent
with eq?
in a Scheme which provides NaN values and
IEEE semantics for them, as (eq? +nan.0 +nan.0)
may
return either #t
or #f
but (=
+nan.0 +nan.0)
must return #f.
This means
that the expected semantics of =
when used with
vector=
are violated. Therefore, the requirement
for consistency has been removed from this version, and the
sample implementation adjusted accordingly.
This SRFI proposes a comprehensive library of vector operations accompanied by a freely available and complete reference implementation. The reference implementation is unencumbered by copyright, and useable with no modifications on any Scheme system that is R5RS-compliant. It also provides several hooks for implementation-specific optimization as well.
R5RS provides very few list-processing procedures, for which reason SRFI 1 exists. However, R5RS provides even fewer vector operations — while it provides mapping, appending, et cetera operations for lists, it specifies only nine vector manipulation operations:
R7RS-small added support for
start and end arguments to vector->list
,
list->vector
and vector-fill!
. It also
provided seven additional vector procedures, bringing vectors
and lists to approximate parity:
SRFI 43 standardized more vector procedures, all of which are included in this SRFI. Unfortunately, R7RS-small and SRFI 43 placed irreconcileable requirements on the procedures invoked by vector-map and vector-for-each. This SRFI resolves that issue by changing these SRFI 43 procedures as well as vector-map!, vector-fold, vector-fold-right, and vector-count to leave out the index argument that is passed under SRFI 43's definition.
In addition, the version of vector-copy
in this SRFI
does not require support for a fill argument, which
makes it equivalent to the R7RS-small definition.
This SRFI also provides the following new procedures (some from Python, some from other sources):
It should be noted that no vector sorting procedures are provided by this SRFI, because there already are several SRFIs for that purpose.
Here is an index of the procedures provided by this package. Those marked by italics are also provided in R7RS-small.
make-vector
vector
vector-unfold
vector-unfold-right
vector-copy
vector-reverse-copy
vector-append
vector-concatenate
vector-append-subvectors
vector?
vector-empty?
vector=
vector-ref
vector-length
vector-fold
vector-fold-right
vector-map
vector-map!
vector-for-each
vector-count
vector-cumulate
vector-index
vector-index-right
vector-skip
vector-skip-right
vector-binary-search
vector-any
vector-every
vector-partition
vector-set!
vector-swap!
vector-fill!
vector-reverse!
vector-copy!
vector-reverse-copy!
vector-unfold!
vector-unfold-right!
vector->list
reverse-vector->list
list->vector
reverse-list->vector
vector->string
string->vector
In this section containing specifications of procedures, the following notation is used to specify parameters and return values:
[start [end]]
It should be noted that all of the procedures that iterate across multiple vectors in parallel stop iterating and produce the final result when the end of the shortest vector is reached. The sole exception is vector=, which automatically returns #f if the vectors' lengths vary.
(make-vector 5 3)
#(3 3 3 3 3)
(vector 0 1 2 3 4)
#(0 1 2 3 4)
unfold
procedure of SRFI 1.
(vector-unfold (λ (i x) (values x (- x 1)))
10 0)
#(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
(vector-unfold values n)
#(0 1 2 ... n-2 n-1)
(vector-unfold (λ (i) (vector-ref vector i))
(vector-length vector))
unfold-right
procedure of SRFI 1.
(vector-unfold-right (λ (i x) (values (cons i x) (+ x 1))) 5 0)
#((0 . 4) (1 . 3) (2 . 2) (3 . 1) (4 . 0))
(vector-unfold-right (λ (i x)
(values (vector-ref vector x)
(+ x 1)))
(vector-length vector)
0)
(vector-copy '#(a b c d e f g h i))
#(a b c d e f g h i)
(vector-copy '#(a b c d e f g h i) 6)
#(g h i)
(vector-copy '#(a b c d e f g h i) 3 6)
#(d e f)
(vector-reverse-copy '#(5 4 3 2 1 0) 1 5)
#(1 2 3 4)
(vector-append '#(x) '#(y))
#(x y)
(vector-append '#(a) '#(b c d))
#(a b c d)
(vector-append '#(a #(b)) '#(#(c)))
#(a #(b) #(c))
(apply vector-append
list-of-vectors)
(vector-concatenate '(#(a b) #(c d)))
#(a b c d)
(vector-append-subvectors '#(a b c d e) 0 2 '#(f g h i j) 2 4)
#(a b h i)
(vector? '#(a b c))
#t
(vector? '(a b c))
#f
(vector? #t)
#f
(vector? '#())
#t
(vector? '())
#f
(vector-empty? '#(a))
#f
(vector-empty? '#(()))
#f
(vector-empty? '#(#()))
#f
(vector-empty? '#())
#t
(vector= eq? '#(a b c d) '#(a b c d))
#t
(vector= eq? '#(a b c d) '#(a b d c))
#f
(vector= = '#(1 2 3 4 5) '#(1 2 3 4))
#f
(vector= = '#(1 2 3 4) '#(1 2 3 4))
#t
(vector= eq?)
#t
(vector= eq? '#(a))
#t
(vector= eq? (vector (vector 'a)) (vector (vector 'a)))
#f
(vector= equal? (vector (vector 'a)) (vector (vector 'a)))
#t
(vector-ref '#(a b c d) 2)
c
(vector-length '#(a b c))
3
(vector-fold (λ (len str)
(max (string-length str) len))
0 vector-of-strings)
(vector-fold (λ (tail elt) (cons elt tail))
'() vec)
(vector-fold (λ (counter n)
(if (even? n) (+ counter 1) counter))
0 vec)
(vector-fold-right (λ (tail elt)
(cons elt tail))
'() '#(a b c d))
(a b c d)
(vector-map (λ (x) (* x x))
(vector-unfold
(λ (i x) (values x (+ x 1)))
4 1))
#(1 4 9 16)
(vector-map (λ (x y) (* x y))
(vector-unfold
(λ (x) (values x (+ x 1)))
5 1)
(vector-unfold
(λ (x) (values x (- x 1)))
5 5))
#(5 8 9 8 5)
(let ((count 0))
(vector-map (λ (ignored-elt)
(set! count (+ count 1))
count)
'#(a b)))
#(1 2) OR #(2 1)
(vector-for-each (λ (x) (display x) (newline))
'#("foo" "bar" "baz" "quux" "zot"))
foo bar baz quux zot
(vector-count even?
'#(3 1 4 1 5 9 2 5 6))
3
(vector-count <
'#(1 3 6 9) '#(2 4 6 8 10 12))
2
Note that the order of arguments to vector-cumulate
was changed by errata-3
on 2016-09-02.
(vector-cumulate + 0 '#(3 1 4 1 5 9 2 5 6))
#(3 4 8 9 14 23 25 30 36)
(vector-index even? '#(3 1 4 1 5 9))
2
(vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
1
(vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
#f
(vector-index
(λ (x1 x2
...)
(not (pred? x1
x1
...)))
vec1 vec2
...)
(vector-skip number? '#(1 2 a b 3 4 c d))
2
(vector-index-right
(λ (x1 x2
...)
(not (pred? x1
x1
...)))
vec1 vec2
...)
Similar to vector-index
and
vector-index-right,
but instead of searching left to right or right to left, this
performs a binary search.
If there is more than one element of vec that matches
value in the sense of cmp,
vector-binary-search
may return the index of any of them.
cmp should be a
procedure of two arguments and return a negative integer, which
indicates that its first argument is less than its second,
zero, which indicates that they are equal, or a positive
integer, which indicates that the first argument is greater
than the second argument. An example cmp might
be:
(λ (char1 char2)
(cond ((char<? char1
char2)
-1)
((char=? char1
char2)
0)
(else 1)))
The sample implementation is in the repository of this SRFI. It has only one non-R5RS dependency: SRFI 23's error procedure, which is also provided by R7RS-small. It is in the public domain, or alternatively under the same copyright as this SRFI. The following files are provided:
vectors-impl.scm
-
a modified version of the implementation of SRFI 43vectors.scm
-
a Chicken library showing what to export for an R5RS implementationvectors.sld
-
an R7RS library that excludes what R7RS-small already providesvectors-test.scm
-
tests using the Chicken test egg (also available on Chibi)These acknowledgements are copied from SRFI 43.
Thanks to Olin Shivers for his wonderfully complete list and string packages; to all the members of the #scheme IRC channel on Freenode who nitpicked a great deal, but also helped quite a lot in general, and helped test the reference implementation in various Scheme systems; to Michael Burschik for his numerous comments; to Sergei Egorov for helping to narrow down the procedures; to Mike Sperber for putting up with an extremely overdue draft; to Felix Winkelmann for continually bugging me about finishing up the SRFI so that it would be only overdue and not withdrawn; and to everyone else who gave questions, comments, thoughts, or merely attention to the SRFI.
Copyright (C) Taylor Campbell (2003). All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.