Lazy
Delay an expression
Section titled “Delay an expression”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(delay (write-u8 65))
When I successfully run stak main.scm
Then the stdout should contain exactly "".
Check if a value is a promise
Section titled “Check if a value is a promise”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(write-u8 (if (promise? <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 |
(delay #f) | A |
(make-promise #f) | A |
Force a promise
Section titled “Force a promise”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(force (delay (write-u8 65)))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Force a promise twice
Section titled “Force a promise twice”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(define x (delay (write-u8 65)))
(force x)(force x)
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Force and delay an expression
Section titled “Force and delay an expression”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(delay-force (write-u8 65))
When I successfully run stak main.scm
Then the stdout should contain exactly "".
Force, delay, and force an expression
Section titled “Force, delay, and force an expression”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(write-u8 (force (delay-force (make-promise (+ 60 5)))))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Force and delay expressions in a loop
Section titled “Force and delay expressions in a loop”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(write-u8 (force (let loop ((i 10000)) (if (zero? i) (make-promise 65) (delay-force (loop (- i 1)))))))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Make a promise
Section titled “Make a promise”Given a file named “main.scm” with:
(import (scheme base) (scheme lazy))
(write-u8 (force (make-promise 65)))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.