Skip to content

Vector

Given a file named “main.scm” with:

(import (scheme base))
(make-vector 42)

When I successfully run stak main.scm

Then the exit status should be 0.

Given a file named “main.scm” with:

(import (scheme base))
(make-vector 42 #t)

When I successfully run stak main.scm

Then the exit status should be 0.

Given a file named “main.scm” with:

(import (scheme base))
(vector-for-each write-u8 #(65 66 67))

When I successfully run stak main.scm

Then the stdout should contain exactly “ABC”.

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (= (vector-length <value>) <length>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

value length
#() 0
#(1) 1
#(1 2) 2
#(1 2 3) 3
(make-vector 3) 3
(make-vector 3 #t) 3
(vector 1 2 3) 3

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (vector-ref <vector> <index>))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

vector index output
(vector 65) 0 A
(vector 65 66) 0 A
(vector 65 66) 1 B
(vector 65 66 67) 0 A
(vector 65 66 67) 1 B
(vector 65 66 67) 2 C

Given a file named “main.scm” with:

(import (scheme base))
(define xs <vector>)
(vector-set! xs <index> 88)
(vector-for-each write-u8 xs)

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

vector index output
(vector 65) 0 X
(vector 65 66) 0 XB
(vector 65 66) 1 AX
(vector 65 66 67) 0 XBC
(vector 65 66 67) 1 AXC
(vector 65 66 67) 2 ABX

Given a file named “main.scm” with:

(import (scheme base))
(vector-for-each write-u8 (vector-append <values>))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

values output
#()
#() #()
#(65) A
#(65) #(66) AB
#(65) #(66) #(67) ABC
#(65) #(66 67) #(68 69 70) ABCDEF

Given a file named “main.scm” with:

(import (scheme base))
(vector-for-each
write-u8
(vector-map + <vectors>))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

vectors output
#(65 66 67) ABC
#(65 66 67) #(0 1 2) ACE
#(65 66 67) #(0 1 2) #(3 4) DG

Given a file named “main.scm” with:

(import (scheme base))
(vector-for-each
(lambda xs (write-u8 (apply + xs)))
<vectors>)

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

vectors output
#()
#(65) A
#(65 66) AB
#(65 66 67) ABC
#(65 66 67) #(0 1 2) ACE
#(65 66 67) #(0 1 2) #(3 4) DG

Given a file named “main.scm” with:

(import (scheme base))
(vector-for-each write-u8 (vector-copy <value>))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

value output
#()
#(65) A
#(65 66) AB
#(65 66 67) ABC

Given a file named “main.scm” with:

(import (scheme base))
(define xs (vector <values>))
(vector-copy! xs <arguments>)
(for-each
(lambda (x) (write-u8 (+ x 65)))
(vector->list xs))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

values arguments output
0 0 #() A
0 1 2 0 #(3 4 5) DEF
0 1 2 1 #(3 4) ADE
0 1 2 2 #(3) ABD
0 1 2 3 4 1 #(5 6 7) AFGHE
0 1 2 3 1 #(4 5 6 7) 1 AFGH
0 1 2 3 1 #(4 5 6 7) 1 3 AFGD

Given a file named “main.scm” with:

(import (scheme base))
(define xs (vector <values>))
(vector-fill! xs <arguments>)
(for-each
(lambda (x) (write-u8 (+ x 65)))
(vector->list xs))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

values arguments output
0 1 B
0 1 2 0 CC
0 1 2 1 AC
0 1 2 3 DDD
0 1 2 3 4 1 3 AEED

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (vector-ref (make-vector <length> 65) <index>))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

length index
1 0
2 0
2 1
3 0
3 1
3 2
8 0
8 1
8 6
8 7
9 0
9 1
9 7
9 8
64 63
65 64
512 511
513 512
4096 4095
4097 4096

Convert values between a list and a vector

Section titled “Convert values between a list and a vector”

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(define xs (iota <count>))
(write-u8 (if (equal? (vector->list (list->vector xs)) xs) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

count
0
1
2
3
4
5
6
7
8
9
16
17
32
33
64
65
128
129
512
513
4096
4097

Given a file named “main.scm” with:

(import (scheme base))
(define xs (include "./value.scm"))
(write-u8 (if (= (vector-ref xs <index>) <index>) 65 66))

And a file named “write.scm” with:

(import (scheme base) (scheme write) (srfi 1))
(write (list->vector (iota <length>)))

And I run the following script:

Terminal window
stak write.scm > value.scm

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

length index
1 0
2 0
2 1
512 0
512 1
512 510
512 511
4096 0
4096 1
4096 4094
4096 4095
8192 0
8192 1
8192 8190
8192 8191