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”.
Examples
Section titled “Examples”input | output |
---|---|
1 | 2 1 |
1 2 | 2 1 4 4 |
1 2 3 | 2 1 4 4 6 9 |
Delete duplicates in a list
Section titled “Delete duplicates in a list”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”.
Examples
Section titled “Examples”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 |
Filter a list
Section titled “Filter a list”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”.
Examples
Section titled “Examples”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 |
Reduce a list
Section titled “Reduce a list”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”.
Examples
Section titled “Examples”elements | output |
---|---|
0 | |
1 | 1 |
1 2 | 3 |
1 2 3 | 6 |
Fold a list from right
Section titled “Fold a list from right”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”.
Examples
Section titled “Examples”elements | output |
---|---|
0 | |
1 | 1 |
1 2 | 3 |
1 2 3 | 6 |
Enumerate numbers
Section titled “Enumerate numbers”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”.
Examples
Section titled “Examples”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 |
Find an element
Section titled “Find an element”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”.
Examples
Section titled “Examples”elements | element |
---|---|
#f | |
1 | #f |
1 3 | #f |
2 | 2 |
1 2 | 2 |
1 3 4 5 | 4 |
Find a tail of a list
Section titled “Find a tail of a list”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”.
Examples
Section titled “Examples”elements | value |
---|---|
#f | |
1 | #f |
1 3 | #f |
2 | ’(2) |
1 2 | ’(2) |
1 3 4 5 | ’(4 5) |
Check if any element meets a condition
Section titled “Check if any element meets a condition”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”.
Examples
Section titled “Examples”elements | value |
---|---|
#f | |
1 | #f |
1 3 | #f |
2 | #t |
1 2 | #t |
1 2 3 4 5 | #t |
Check if every element meets a condition
Section titled “Check if every element meets a condition”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”.
Examples
Section titled “Examples”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 |
Calculate an index of an element
Section titled “Calculate an index of an element”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”.
Examples
Section titled “Examples”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 |
Reduce numbers
Section titled “Reduce numbers”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”.
Examples
Section titled “Examples”elements | output |
---|---|
42 | |
0 | 0 |
1 | 1 |
1 2 | 3 |
1 2 3 | 6 |
Reduce numbers from right
Section titled “Reduce numbers from right”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”.
Examples
Section titled “Examples”elements | output |
---|---|
42 | |
0 | 0 |
1 | 1 |
1 2 | 3 |
1 2 3 | 6 |
Get the fourth tail of a list
Section titled “Get the fourth tail of a list”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”.