srfi-216

  1. srfi-216
    1. Introduction
    2. Author
    3. Repository
    4. API
      1. Booleans
      2. The empty list
      3. Time data
      4. Random numbers
      5. Multi-threading
      6. Streams
    5. Example
    6. License
    7. Version history
      1. 0.1

Introduction

This egg implements SRFI-216, with minor modifications to the reference implementation to avoid unnecessary dependencies.

Author

Vasilij Schneidermann

Repository

https://depp.brause.cc/srfi-216

API

Booleans

[constant] false

The canonical false value #f.

Even though Scheme reports as early as R4RS (Sections 3.2, 6.1) already have #f as a distinct false value, SICP continues to refer to false. See Section 1.1.6, footnote 17.

[constant] true

The canonical true value #t.

Even though Scheme reports as early as R4RS (Sections 3.2, 6.1) already have #t as a distinct true value, SICP continues to refer to true. See Section 1.1.6, footnote 17.

The empty list

[constant] nil

The canonical empty list value '()

Remark: even though many modern Scheme implementations only use '() to represent the empty list, SICP follows the Lisp tradition in this respect.

Time data

[procedure] (runtime)

Returns an integer that specifies the amount of time the process has been running (measured in microseconds). See Section 1.2.6 of the SICP, Subsection "Probabilistic Methods", Exercise 1.22.

Example:

(import (srfi 216))
(display (runtime))
;; prints 1604464095599.357

Random numbers

[procedure] (random x)

Returns a nonnegative number less than its input. If random is given an exact integer, it returns an exact integer, but if it is given a decimal value, it returns a decimal value. If x is less than 0, the behaviour is unspecified. See Section 1.2.6 of the SICP, Subsection "Fermat Test", and Exercise 3.5, footnote 8.

Example:

(import (srfi 216))
(random 11)
;; prints 1

Multi-threading

[procedure] (parallel-execute p1 p2 ...)

Each argument must be a procedure of no arguments. parallel-execute creates a separate process for each argument, and that process applies the argument to no arguments. These processes all run concurrently.

See SICP Section 3.4.2, Subsection "Serializers in Scheme".

Example:

(import (srfi 216))
(define x 10)
(parallel-execute
  (lambda () (set! x (* x x))) ; P1
  (lambda () (set! x (+ x 1)))) ; P2
;; May assign to x any of the following:
;; 101 - P1 sets x to 100 and then P2 increments x to 101.
;; 121 - P2 increments x to 11 and then P1 sets x to x * x.
;; 110 - P2 changes x from 10 to 11 between the two times that P1 accesses the value of x during the evaluation of (* x x).
;; 11 - P2 accesses x, then P1 sets x to 100, then P2 sets x.
;; 100 - P1 accesses x (twice), then P2 sets x to 11, then P 1 sets x.
[procedure] (test-and-set! cell)

Tests the cell and returns the result of the test. In addition, if the test was false, test-and-set! sets the cell contents to true before returning false.

The test-and-set! operation must be performed atomically. That is, the implementation must guarantee that, once a process has tested the cell and found it to be false, the cell contents will actually be set to true before any other process can test the cell. See Section 3.4.2, Subsection "Implementing Serializers".

Streams

[syntax] (cons-stream a b)

Is equivalent to (cons a (delay b)). See SICP Section 3.5.1.

Remark: The necessity to include cons-stream is due to the fact that SICP does not introduce any syntax-altering constructs beyond writing your own metacircular interpreter. Please note that the stream-cons procedure from SRFI 41 (implementing "even" streams) would not be a drop-in replacement for cons-stream.

[constant] the-empty-stream

A distinguishable object that cannot be the result of any cons-stream operation. See SICP Section 3.5.1, footnote 54.

Equivalent to the empty list.

[procedure] (stream-null? x)

Returns the value of true if x is the-empty-stream, and the value of false otherwise. See SICP Section 3.5.1, footnote 54.

Example:

(import (srfi 126))
(stream-null? the-empty-stream)
;; => #t
(stream-null? (cons-stream 'a 'b))
;; => #f

Equivalent to null?.

Example

(import scheme)
(import (srfi 216))

(define (prime? n) ...)

(define (timed-prime-test n)
  (newline)
  (display n)
  (start-prime-test n (runtime)))

(define (start-prime-test n start-time)
  (if (prime? n)
      (report-prime (- (runtime) start-time))))

(define (report-prime elapsed-time)
  (display " *** ")
  (display elapsed-time))

License

MIT

Version history

0.1