Number
Check if a value is a number
Section titled “Check if a value is a number”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (number? <value>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| value | output |
|---|---|
| #f | B |
| ’() | B |
| 0 | A |
| 42 | A |
| -2045 | A |
Check a number trait
Section titled “Check a number trait”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (<predicate> <value>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| predicate | value | output |
|---|---|---|
| zero? | 0 | A |
| zero? | 1 | B |
| positive? | 42 | A |
| positive? | -42 | B |
| negative? | -42 | A |
| negative? | 42 | B |
| even? | 2 | A |
| even? | 3 | B |
| odd? | 3 | A |
| odd? | 2 | B |
Check a number class of integers
Section titled “Check a number class of integers”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (<predicate> <value>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| predicate | value | output |
|---|---|---|
| number? | 0 | A |
| number? | 1 | A |
| number? | -42 | A |
| complex? | 0 | A |
| complex? | 1 | A |
| complex? | -42 | A |
| real? | 0 | A |
| real? | 1 | A |
| real? | -42 | A |
| rational? | 0 | A |
| rational? | 1 | A |
| rational? | -42 | A |
| integer? | 0 | A |
| integer? | 1 | A |
| integer? | -42 | A |
Check a number class of non-numbers
Section titled “Check a number class of non-numbers”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (<predicate> #f) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “B”.
Examples
Section titled “Examples”| predicate |
|---|
| number? |
| complex? |
| real? |
| rational? |
| integer? |
Use literals
Section titled “Use literals”Given a file named “main.scm” with:
(import (scheme base))
(define x <value>)When I successfully run stak main.scm
Then the exit status should be 0.
Examples
Section titled “Examples”| value |
|---|
| 0 |
| 1 |
| 42 |
| -1 |
| -42 |
Use a negative integer
Section titled “Use a negative integer”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (+ 66 -1))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Use large (but not big) integers
Section titled “Use large (but not big) integers”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (- 1065 1000))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Use integers around the encoding base
Section titled “Use integers around the encoding base”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (- <value> 60))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| value | output |
|---|---|
| 127 | C |
| 128 | D |
| 129 | E |
Use arithmetic operators
Section titled “Use arithmetic operators”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= <expression> <value>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| expression | value |
|---|---|
| (+) | 0 |
| (+ 1) | 1 |
| (+ 1 2) | 3 |
| (- 1) | -1 |
| (- 0 1) | -1 |
| (- 0 1 2) | -3 |
| (*) | 1 |
| (* 2) | 2 |
| (* 2 3) | 6 |
| (/ 6 2) | 3 |
| (/ 6 2 3) | 1 |
Use division operators
Section titled “Use division operators”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= <expression> <value>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| expression | value |
|---|---|
| (quotient 6 2) | 3 |
| (quotient 6 3) | 2 |
| (quotient -6 2) | -3 |
| (quotient -6 -2) | 3 |
| (truncate-quotient 6 2) | 3 |
| (truncate-quotient 6 3) | 2 |
| (truncate-quotient -6 2) | -3 |
| (truncate-quotient -6 -2) | 3 |
| (remainder 8 3) | 2 |
| (remainder 8 -3) | 2 |
| (remainder -8 3) | -2 |
| (remainder -8 -3) | -2 |
| (truncate-remainder 8 3) | 2 |
| (truncate-remainder 8 -3) | 2 |
| (truncate-remainder -8 3) | -2 |
| (truncate-remainder -8 -3) | -2 |
| (modulo 5 1) | 0 |
| (modulo 5 2) | 1 |
| (modulo 8 3) | 2 |
| (modulo 8 -3) | -1 |
| (modulo -8 3) | 1 |
| (modulo -8 -3) | -2 |
| (floor-quotient 5 1) | 5 |
| (floor-quotient 5 2) | 2 |
| (floor-quotient 5 3) | 1 |
| (floor-quotient -5 2) | -3 |
| (floor-quotient -5 -2) | 2 |
| (floor-remainder 5 1) | 0 |
| (floor-remainder 5 2) | 1 |
| (floor-remainder 5 3) | 2 |
| (floor-remainder -5 2) | 1 |
| (floor-remainder -5 -2) | -1 |
| (floor-remainder 6 2) | 0 |
| (floor-remainder 6 -2) | 0 |
| (floor-remainder -6 2) | 0 |
| (floor-remainder -6 -2) | 0 |
Use multi-value division operators
Section titled “Use multi-value division operators”Given a file named “main.scm” with:
(import (scheme base))
(define-values (x y) <expression>)
(write-u8 (if (= x <quotient>) 65 66))(write-u8 (if (= y <remainder>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “AA”.
Examples
Section titled “Examples”| expression | quotient | remainder |
|---|---|---|
| (floor/ 8 3) | 2 | 2 |
| (floor/ 8 -3) | -3 | -1 |
| (floor/ -8 3) | -3 | 1 |
| (floor/ -8 -3) | 2 | -2 |
| (truncate/ 8 3) | 2 | 2 |
| (truncate/ 8 -3) | -2 | 2 |
| (truncate/ -8 3) | -2 | -2 |
| (truncate/ -8 -3) | 2 | -2 |
Calculate a multiplicative inverse
Section titled “Calculate a multiplicative inverse”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (/ 2) (/ 1 2)) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Calculate a square
Section titled “Calculate a square”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (square <value>) <result>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| value | result |
|---|---|
| -3 | 9 |
| -2 | 4 |
| -1 | 1 |
| 0 | 0 |
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
Calculate a square root of an exact integer
Section titled “Calculate a square root of an exact integer”Given a file named “main.scm” with:
(import (scheme base))
(define-values (x y) (exact-integer-sqrt <value>))
(write-u8 (if (= x <root>) 65 66))(write-u8 (if (= y <remainder>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “AA”.
Examples
Section titled “Examples”| value | root | remainder |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 0 |
| 4 | 2 | 0 |
| 5 | 2 | 1 |
| 8 | 2 | 4 |
| 9 | 3 | 0 |
Calculate the greatest common divisor
Section titled “Calculate the greatest common divisor”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (gcd <x> <y>) <z>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| x | y | z |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 6 | 2 | 2 |
| 2 | 6 | 2 |
| 6 | 3 | 3 |
| 3 | 6 | 3 |
| 30 | 42 | 6 |
| 42 | 30 | 6 |
| -30 | 42 | 6 |
| -30 | -42 | 6 |
Calculate the least common multiple
Section titled “Calculate the least common multiple”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (lcm <x> <y>) <z>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| x | y | z |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 3 | 6 |
| 3 | 2 | 6 |
| 6 | 10 | 30 |
| 10 | 6 | 30 |
| -6 | 10 | 30 |
| -6 | -10 | 30 |
Calculate a numerator
Section titled “Calculate a numerator”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (numerator 42) 42) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Calculate a denominator
Section titled “Calculate a denominator”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (denominator 42) 1) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Compare numbers
Section titled “Compare numbers”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if <expression> 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| expression |
|---|
| (< 0 1) |
| (< 0 1 2) |
| (> 1 0) |
| (<= 0 1) |
| (<= 0 0) |
| (>= 1 0) |
| (>= 0 0) |
Compare numbers with an insufficient number of arguments
Section titled “Compare numbers with an insufficient number of arguments”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (< <values>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| values |
|---|
| 0 |
Calculate a minimum
Section titled “Calculate a minimum”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (min <values>) <output>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| values | output |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 1 2 | 1 |
| 1 2 3 | 1 |
| 2 3 1 | 1 |
| 3 1 2 | 1 |
Calculate a maximum
Section titled “Calculate a maximum”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (max <values>) <output>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| values | output |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 1 2 | 2 |
| 1 2 3 | 3 |
| 2 3 1 | 3 |
| 3 1 2 | 3 |
Convert a number to a string
Section titled “Convert a number to a string”Given a file named “main.scm” with:
(import (scheme base))
(write-string (number->string <value> <radix>))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| value | radix | output |
|---|---|---|
| 0 | 0 | |
| 1 | 1 | |
| 2 | 2 | |
| 42 | 42 | |
| -1 | -1 | |
| -2 | -2 | |
| -42 | -42 | |
| 0 | 16 | 0 |
| 1 | 16 | 1 |
| 2 | 16 | 2 |
| 15 | 16 | f |
| 42 | 16 | 2a |
| -1 | 16 | -1 |
| -2 | 16 | -2 |
| -42 | 16 | -2a |
| 0 | 32 | 0 |
| 1 | 32 | 1 |
| 2 | 32 | 2 |
| 31 | 32 | v |
| 42 | 32 | 1a |
| -1 | 32 | -1 |
| -2 | 32 | -2 |
| -42 | 32 | -1a |
Convert a string to a number
Section titled “Convert a string to a number”Given a file named “main.scm” with:
(import (scheme base))
(write-string (number->string (string->number "<value>" <radix>)))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| value | radix | output |
|---|---|---|
| 0 | 0 | |
| 1 | 1 | |
| 2 | 2 | |
| 42 | 42 | |
| -1 | -1 | |
| -2 | -2 | |
| -42 | -42 | |
| 0 | 16 | 0 |
| 1 | 16 | 1 |
| 2 | 16 | 2 |
| f | 16 | 15 |
| 2a | 16 | 42 |
| 2A | 16 | 42 |
| -1 | 16 | -1 |
| -2 | 16 | -2 |
| -2a | 16 | -42 |
| -2A | 16 | -42 |
| 0 | 32 | 0 |
| 1 | 32 | 1 |
| 2 | 32 | 2 |
| v | 32 | 31 |
| 1a | 32 | 42 |
| 1A | 32 | 42 |
| -1 | 32 | -1 |
| -2 | 32 | -2 |
| -1a | 32 | -42 |
| -1A | 32 | -42 |
Convert an invalid string to a number
Section titled “Convert an invalid string to a number”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (string->number "<value>") 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “B”.
Examples
Section titled “Examples”| value |
|---|
| x |
| foo |
| - |
| 2x |
| -x |
| -2x |