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 srfi133@nospamsrfi.schemers.org
. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.
vectorunfold!
)vectorcumulate
from (vectorcumulate f vec knil)
to
(vectorcumulate f knil vec)
to match the
argument order of vectorfold.)vectorpartition
.)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 R5RScompliant. It also provides several hooks for implementationspecific optimization as well.
R5RS provides very few listprocessing 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:
R7RSsmall added support for
start and end arguments to vector>list
,
list>vector
and vectorfill!
. 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, R7RSsmall and SRFI 43 placed irreconcileable requirements on the procedures invoked by vectormap and vectorforeach. This SRFI resolves that issue by changing these SRFI 43 procedures as well as vectormap!, vectorfold, vectorfoldright, and vectorcount to leave out the index argument that is passed under SRFI 43's definition.
In addition, the version of vectorcopy
in this SRFI
does not require support for a fill argument, which
makes it equivalent to the R7RSsmall 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 R7RSsmall.
makevector
vector
vectorunfold
vectorunfoldright
vectorcopy
vectorreversecopy
vectorappend
vectorconcatenate
vectorappendsubvectors
vector?
vectorempty?
vector=
vectorref
vectorlength
vectorfold
vectorfoldright
vectormap
vectormap!
vectorforeach
vectorcount
vectorcumulate
vectorindex
vectorindexright
vectorskip
vectorskipright
vectorbinarysearch
vectorany
vectorevery
vectorpartition
vectorset!
vectorswap!
vectorfill!
vectorreverse!
vectorcopy!
vectorreversecopy!
vectorunfold!
vectorunfoldright!
vector>list
reversevector>list
list>vector
reverselist>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.
(makevector 5 3)
#(3 3 3 3 3)
(vector 0 1 2 3 4)
#(0 1 2 3 4)
unfold
procedure of SRFI 1.
(vectorunfold (λ (i x) (values x ( x 1)))
10 0)
#(0 1 2 3 4 5 6 7 8 9)
(vectorunfold values n)
#(0 1 2 ... n2 n1)
(vectorunfold (λ (i) (vectorref vector i))
(vectorlength vector))
unfoldright
procedure of SRFI 1.
(vectorunfoldright (λ (i x) (values (cons i x) (+ x 1))) 5 0)
#((0 . 4) (1 . 3) (2 . 2) (3 . 1) (4 . 0))
(vectorunfoldright (λ (i x)
(values (vectorref vector x)
(+ x 1)))
(vectorlength vector)
0)
(vectorcopy '#(a b c d e f g h i))
#(a b c d e f g h i)
(vectorcopy '#(a b c d e f g h i) 6)
#(g h i)
(vectorcopy '#(a b c d e f g h i) 3 6)
#(d e f)
(vectorreversecopy '#(5 4 3 2 1 0) 1 5)
#(1 2 3 4)
(vectorappend '#(x) '#(y))
#(x y)
(vectorappend '#(a) '#(b c d))
#(a b c d)
(vectorappend '#(a #(b)) '#(#(c)))
#(a #(b) #(c))
(apply vectorappend
listofvectors)
(vectorconcatenate '(#(a b) #(c d)))
#(a b c d)
(vectorappendsubvectors '#(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
(vectorempty? '#(a))
#f
(vectorempty? '#(()))
#f
(vectorempty? '#(#()))
#f
(vectorempty? '#())
#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
(vectorref '#(a b c d) 2)
c
(vectorlength '#(a b c))
3
(vectorfold (λ (len str)
(max (stringlength str) len))
0 vectorofstrings)
(vectorfold (λ (tail elt) (cons elt tail))
'() vec)
(vectorfold (λ (counter n)
(if (even? n) (+ counter 1) counter))
0 vec)
(vectorfoldright (λ (tail elt)
(cons elt tail))
'() '#(a b c d))
(a b c d)
(vectormap (λ (x) (* x x))
(vectorunfold
(λ (i x) (values x (+ x 1)))
4 1))
#(1 4 9 16)
(vectormap (λ (x y) (* x y))
(vectorunfold
(λ (x) (values x (+ x 1)))
5 1)
(vectorunfold
(λ (x) (values x ( x 1)))
5 5))
#(5 8 9 8 5)
(let ((count 0))
(vectormap (λ (ignoredelt)
(set! count (+ count 1))
count)
'#(a b)))
#(1 2) OR #(2 1)
(vectorforeach (λ (x) (display x) (newline))
'#("foo" "bar" "baz" "quux" "zot"))
foo bar baz quux zot
(vectorcount even?
'#(3 1 4 1 5 9 2 5 6))
3
(vectorcount <
'#(1 3 6 9) '#(2 4 6 8 10 12))
2
Note that the order of arguments to vectorcumulate
was changed by errata3
on 2016/9/2.
(vectorcumulate + 0 '#(3 1 4 1 5 9 2 5 6))
#(3 4 8 9 14 23 25 30 36)
(vectorindex even? '#(3 1 4 1 5 9))
2
(vectorindex < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
1
(vectorindex = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
#f
(vectorindex
(λ (x_{1} x_{2}
...)
(not (pred? x_{1}
x_{1}
...)))
vec_{1} vec_{2}
...)
(vectorskip number? '#(1 2 a b 3 4 c d))
2
(vectorindexright
(λ (x_{1} x_{2}
...)
(not (pred? x_{1}
x_{1}
...)))
vec_{1} vec_{2}
...)
vectorbinarysearch
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:
(λ (char_{1} char_{2})
(cond ((char<? char_{1}
char_{2})
1)
((char=? char_{1}
char_{2})
0)
(else 1)))
The sample implementation is in the repository of this SRFI. It has only one nonR5RS dependency: SRFI 23's error procedure, which is also provided by R7RSsmall. It is in the public domain, or alternatively under the same copyright as this SRFI. The following files are provided:
vectorsimpl.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 R7RSsmall already providesvectorstest.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.