|
| 1 | +;;; SRFI 216 |
| 2 | +;;; SICP Prerequisites |
| 3 | +;;; |
| 4 | +;;; This SRFI provides "out-of-the-box" support for hosting the exercises suggested by |
| 5 | +;;; "Structure and Interpretation of Computer Programs". It primarily provides procedures |
| 6 | +;;; for working with time data and streams, as well as SICP names for true and false. |
| 7 | +;;; Support for multi-threading is omitted due to LispKit currently not supporting threads. |
| 8 | +;;; None of the provided procedures are fit for production use. They are only designed for |
| 9 | +;;; pedagogical purposes. Students are expected to be able to just write |
| 10 | +;;; |
| 11 | +;;; (include (srfi sicp)) |
| 12 | +;;; |
| 13 | +;;; and have the code from the book run without problems (apart from those intended by the |
| 14 | +;;; book authors). |
| 15 | +;;; |
| 16 | +;;; Companion website of SICP: https://mitpress.mit.edu/sites/default/files/sicp/index.html |
| 17 | +;;; |
| 18 | +;;; Copyright © 2020 Vladimir Nikishkin. All rights reserved. |
| 19 | +;;; |
| 20 | +;;; Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 21 | +;;; software and associated documentation files (the "Software"), to deal in the Software |
| 22 | +;;; without restriction, including without limitation the rights to use, copy, modify, merge, |
| 23 | +;;; publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 24 | +;;; persons to whom the Software is furnished to do so, subject to the following conditions: |
| 25 | +;;; |
| 26 | +;;; The above copyright notice and this permission notice shall be included in all copies or |
| 27 | +;;; substantial portions of the Software. |
| 28 | +;;; |
| 29 | +;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 30 | +;;; INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 31 | +;;; PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 32 | +;;; FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 33 | +;;; OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 34 | +;;; DEALINGS IN THE SOFTWARE. |
| 35 | +;;; |
| 36 | +;;; Adaptation to LispKit |
| 37 | +;;; Copyright © 2021 Matthias Zenger. All rights reserved. |
| 38 | + |
| 39 | +(define-library (srfi 216) |
| 40 | + |
| 41 | + (export runtime |
| 42 | + random |
| 43 | + ; parallel-execute |
| 44 | + ; test-and-set! |
| 45 | + cons-stream |
| 46 | + stream-null? |
| 47 | + the-empty-stream |
| 48 | + true |
| 49 | + false |
| 50 | + nil) |
| 51 | + |
| 52 | + (import (lispkit base) |
| 53 | + (except (lispkit stream) stream-null?) |
| 54 | + (only (srfi 27) random-integer random-real)) |
| 55 | + ; (only (srfi 18) thread-start! make-thread thread-join! |
| 56 | + ; make-mutex mutex-lock! mutex-unlock!) |
| 57 | + |
| 58 | + (begin |
| 59 | + |
| 60 | + ;;; r4rs booleans |
| 61 | + |
| 62 | + (define true #t) |
| 63 | + (define false #f) ;; luckily, SICP does not use '() as false |
| 64 | + |
| 65 | + ;;; Empty list |
| 66 | + (define nil '()) |
| 67 | + |
| 68 | + ;;; Random numbers |
| 69 | + |
| 70 | + (define (random x) ;; srfi-27 |
| 71 | + (if (exact-integer? x) |
| 72 | + (random-integer x) |
| 73 | + (* x (random-real)))) |
| 74 | + |
| 75 | + ;;; Timing |
| 76 | + |
| 77 | + (define (runtime) ;; r7rs |
| 78 | + (round (* (current-jiffy) (jiffies-per-second) #e1e6))) ;; microseconds |
| 79 | + |
| 80 | + ;;; Multi-threading |
| 81 | + |
| 82 | + ; (define (parallel-execute . forms) ;; srfi-18 |
| 83 | + ; (let ((myo (open-output-string))) |
| 84 | + ; (define (create-threads . forms) |
| 85 | + ; (if (null? forms) |
| 86 | + ; (list) |
| 87 | + ; (let ((ctxi (thread-start! |
| 88 | + ; (make-thread |
| 89 | + ; (lambda () (parameterize ((current-output-port myo)) |
| 90 | + ; ((car forms)))))))) |
| 91 | + ; (cons ctxi (apply create-threads (cdr forms)))))) |
| 92 | + ; (define (wait-threads thread-list) |
| 93 | + ; (if (null? thread-list) |
| 94 | + ; #t |
| 95 | + ; (begin (thread-join! (car thread-list)) |
| 96 | + ; (wait-threads (cdr thread-list))))) |
| 97 | + ; (wait-threads (apply create-threads forms)) |
| 98 | + ; (display (get-output-string myo)))) ;; return value is not specified by SICP |
| 99 | + ; |
| 100 | + ; (define central-old-mutex (make-mutex 'global-srfi-18-mutex)) ;; not exported |
| 101 | + ; |
| 102 | + ; (define (test-and-set! cell) ;; srfi-18 |
| 103 | + ; (mutex-lock! central-old-mutex) |
| 104 | + ; (let ((output (if (car cell) #t (begin (set-car! cell #t) #f)))) |
| 105 | + ; (mutex-unlock! central-old-mutex) |
| 106 | + ; output)) |
| 107 | + |
| 108 | + ;;; Streams |
| 109 | + |
| 110 | + (define-syntax cons-stream ;; r7rs |
| 111 | + (syntax-rules () |
| 112 | + ((cons-stream a b) (cons a (delay b))))) |
| 113 | + |
| 114 | + (define stream-null? null?) |
| 115 | + |
| 116 | + (define the-empty-stream '()) |
| 117 | + ) |
| 118 | +) |
0 commit comments