Skip to content

SRFI 1

Map a function on a list and append elements

Section titled “Map a function on a list and append elements”

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8
(if (equal?
(append-map (lambda (x) (list (* 2 x) (* x x))) '(<input>))
'(<output>))
65
66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
1 2 1
1 2 2 1 4 4
1 2 3 2 1 4 4 6 9

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (delete-duplicates '(<input>)) '(<output>)) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
1 1
1 1 1
1 1 2 2 1 2
1 2 1 2 1 2
1 2 1 3 3 2 3 1 2 3

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (filter odd? '(<input>)) '(<output>)) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
1 1
2
1 2 1
1 2 3 1 3
1 2 3 4 1 3
1 2 3 4 5 6 1 3 5

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (fold + 0 '(<elements>)) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements output
0
1 1
1 2 3
1 2 3 6

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (fold-right + 0 '(<elements>)) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements output
0
1 1
1 2 3
1 2 3 6

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (iota <arguments>) '(<elements>)) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

arguments elements
0
1 0
2 0 1
3 0 1 2
1 1 1
1 2 2
3 5 5 6 7
3 5 2 5 7 9

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (eq? (find even? '(<elements>)) <element>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements element
#f
1 #f
1 3 #f
2 2
1 2 2
1 3 4 5 4

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (find-tail even? '(<elements>)) <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements value
#f
1 #f
1 3 #f
2 ’(2)
1 2 ’(2)
1 3 4 5 ’(4 5)

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (any even? '(<elements>)) <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements value
#f
1 #f
1 3 #f
2 #t
1 2 #t
1 2 3 4 5 #t

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (every even? '(<elements>)) <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements value
#t
1 #f
1 3 #f
2 #t
1 2 #f
2 4 6 #t
2 4 5 #f
1 2 3 4 5 #f

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (list-index <predicate> <lists>) <index>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

predicate lists index
even? ’() #f
even? ’(1) #f
even? ’(1 3) #f
even? ’(2) 0
even? ’(1 2) 1
even? ’(1 2 3) 1
even? ’(1 3 2) 2
= ’(0 2) ’(1 2 3) 1
= ’(2 1 3) ’(1 2 3) 2

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (reduce + 42 '(<elements>)) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements output
42
0 0
1 1
1 2 3
1 2 3 6

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (equal? (reduce-right + 42 '(<elements>)) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

elements output
42
0 0
1 1
1 2 3
1 2 3 6

Given a file named “main.scm” with:

(import (scheme base) (srfi 1))
(write-u8 (if (eq? (cddddr '(x x x x)) '()) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.