Skip to content

Commit 9bbb4f6

Browse files
committed
Support SRFI 216.
1 parent 08e1794 commit 9bbb4f6

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

LispKit.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
CC1DDB6626A9BB430049E99D /* SRFI-222.scm in Copy tests */ = {isa = PBXBuildFile; fileRef = CC1DDB6326A9B9D30049E99D /* SRFI-222.scm */; };
9090
CC1DDB6726A9BB5B0049E99D /* 222.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC1DDB6426A9BA2F0049E99D /* 222.sld */; };
9191
CC1DDB6826A9BB840049E99D /* SRFI-222.scm in Copy tests */ = {isa = PBXBuildFile; fileRef = CC1DDB6326A9B9D30049E99D /* SRFI-222.scm */; };
92+
CC1DDB6A26AA34E40049E99D /* 216.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC1DDB6926AA34780049E99D /* 216.sld */; };
93+
CC1DDB6B26AA35000049E99D /* 216.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC1DDB6926AA34780049E99D /* 216.sld */; };
9294
CC1EA5C9214E6CD8006BBE7E /* Turtle.scm in Copy examples */ = {isa = PBXBuildFile; fileRef = CC1EA5C8214E6BF3006BBE7E /* Turtle.scm */; };
9395
CC1EA5CB214EF30E006BBE7E /* 161.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC1EA5CA214EEF41006BBE7E /* 161.sld */; };
9496
CC201A6D1DBD72C100045A4D /* LibraryRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC201A6C1DBD72C100045A4D /* LibraryRegistry.swift */; };
@@ -1438,6 +1440,7 @@
14381440
dstPath = Root/LispKit/Libraries/srfi;
14391441
dstSubfolderSpec = 7;
14401442
files = (
1443+
CC1DDB6A26AA34E40049E99D /* 216.sld in Copy pre-installed SRFI libraries */,
14411444
CC1DDB6526A9BB200049E99D /* 222.sld in Copy pre-installed SRFI libraries */,
14421445
CC0E57C4268FAAC100693DD2 /* 223.sld in Copy pre-installed SRFI libraries */,
14431446
CC0E57BE268BB88700693DD2 /* 221.sld in Copy pre-installed SRFI libraries */,
@@ -1532,6 +1535,7 @@
15321535
dstPath = Root/LispKit/Libraries/srfi;
15331536
dstSubfolderSpec = 7;
15341537
files = (
1538+
CC1DDB6B26AA35000049E99D /* 216.sld in Copy pre-installed SRFI libraries */,
15351539
CC1DDB6726A9BB5B0049E99D /* 222.sld in Copy pre-installed SRFI libraries */,
15361540
CC0E57C6268FAAE800693DD2 /* 223.sld in Copy pre-installed SRFI libraries */,
15371541
CC0E57C0268BB8BE00693DD2 /* 221.sld in Copy pre-installed SRFI libraries */,
@@ -2154,6 +2158,7 @@
21542158
CC1D6CC1258CDF0000D36645 /* SRFI-209.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = "SRFI-209.scm"; sourceTree = "<group>"; };
21552159
CC1DDB6326A9B9D30049E99D /* SRFI-222.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = "SRFI-222.scm"; sourceTree = "<group>"; };
21562160
CC1DDB6426A9BA2F0049E99D /* 222.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = 222.sld; sourceTree = "<group>"; };
2161+
CC1DDB6926AA34780049E99D /* 216.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = 216.sld; sourceTree = "<group>"; };
21572162
CC1EA5C8214E6BF3006BBE7E /* Turtle.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = Turtle.scm; sourceTree = "<group>"; };
21582163
CC1EA5CA214EEF41006BBE7E /* 161.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = 161.sld; sourceTree = "<group>"; };
21592164
CC201A6C1DBD72C100045A4D /* LibraryRegistry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LibraryRegistry.swift; sourceTree = "<group>"; };
@@ -2997,6 +3002,7 @@
29973002
CC1D6CBD258CDE3700D36645 /* 209.sld */,
29983003
CC0D7B382544AF2600FB9301 /* 210.sld */,
29993004
CCD5E559262E31AE00C45EA0 /* 214.sld */,
3005+
CC1DDB6926AA34780049E99D /* 216.sld */,
30003006
CC879885264D4739006950A7 /* 219.sld */,
30013007
CC0E57BC268BB7C900693DD2 /* 221.sld */,
30023008
CC1DDB6426A9BA2F0049E99D /* 222.sld */,
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)