AtlatestRepositorysigil-r7rs

sigil-r7rs / tree / src / schemebase.sgl

1;;; (scheme base) - R7RS Base Library
2;;;
3;;; The core R7RS library containing fundamental procedures, syntax, and values.
4;;; This provides the standard Scheme interface for portable code.
5;;;
6;;; See: [R7RS Small](https://small.r7rs.org/) §6.1-6.14
7;;;
8;;; For Sigil-specific extensions, use `(sigil core)` instead.
9
10(define-library (scheme base)
11 (import (sigil io)
12 (sigil math)
13 (sigil string)
14 (sigil list)
15 (sigil core)
16 (srfi srfi-9))
17 (export
18 ;; Equivalence predicates
19 eq?
20 eqv?
21 equal?
23 ;; Booleans
24 not
25 boolean?
26 boolean=?
28 ;; Pairs and lists
29 pair?
30 cons
31 car
32 cdr
33 set-car!
34 set-cdr!
35 caar cadr cdar cddr
36 null?
37 list?
38 list
39 make-list
40 length
41 append
42 reverse
43 list-ref
44 list-tail
45 list-set!
46 memq
47 memv
48 member
49 assq
50 assv
51 assoc
52 list-copy
53 map
54 for-each
56 ;; Symbols
57 symbol?
58 symbol=?
59 symbol->string
60 string->symbol
62 ;; Numbers
63 number?
64 integer?
65 exact-integer?
66 real?
67 exact?
68 inexact?
69 exact
70 inexact
71 =
72 <
73 >
74 <=
75 >=
76 zero?
77 positive?
78 negative?
79 odd?
80 even?
81 max
82 min
83 +
84 -
85 *
86 /
87 abs
88 floor
89 ceiling
90 truncate
91 round
92 quotient
93 remainder
94 modulo
95 gcd
96 lcm
97 expt
98 square
99 floor-quotient
100 floor-remainder
101 floor/
102 truncate-quotient
103 truncate-remainder
104 truncate/
105 number->string
106 string->number
108 ;; Characters
109 char?
110 char=?
111 char<?
112 char>?
113 char<=?
114 char>=?
115 char->integer
116 integer->char
118 ;; Strings
119 string?
120 make-string
121 string
122 string-length
123 string-ref
124 string-set!
125 string=?
126 string<?
127 string>?
128 string<=?
129 string>=?
130 substring
131 string-append
132 string-copy
133 string-copy!
134 string-fill!
135 string-upcase
136 string-downcase
137 string-foldcase
138 string->list
139 list->string
140 string-for-each
141 string-map
142 vector->string
143 string->vector
145 ;; Vectors
146 vector?
147 make-vector
148 vector
149 vector-length
150 vector-ref
151 vector-set!
152 vector->list
153 list->vector
154 vector-copy
155 vector-copy!
156 vector-fill!
157 vector-for-each
158 vector-map
160 ;; Bytevectors
161 bytevector?
162 make-bytevector
163 bytevector
164 bytevector-length
165 bytevector-u8-ref
166 bytevector-u8-set!
167 bytevector-copy
168 bytevector-copy!
169 bytevector-append
170 utf8->string
171 string->utf8
173 ;; Control features
174 procedure?
175 apply
176 call-with-current-continuation
177 call/cc
178 values
179 call-with-values
180 dynamic-wind
182 ;; Multiple values
183 let-values
184 let*-values
185 define-values
187 ;; Exceptions
188 error
189 error-object?
190 error-object-message
191 error-object-irritants
192 error-object-type
193 file-error?
194 read-error?
195 raise
196 raise-continuable
197 guard
198 with-exception-handler
200 ;; I/O
201 input-port?
202 output-port?
203 textual-port?
204 binary-port?
205 port?
206 current-input-port
207 current-output-port
208 current-error-port
209 close-port
210 close-input-port
211 close-output-port
212 port-open?
213 input-port-open?
214 output-port-open?
215 open-input-string
216 open-output-string
217 get-output-string
218 flush-output-port
219 read-char
220 peek-char
221 read-line
222 read-string
223 read-u8
224 peek-u8
225 write-u8
226 read-bytevector
227 read-bytevector!
228 write-bytevector
229 open-input-bytevector
230 open-output-bytevector
231 get-output-bytevector
232 char-ready?
233 u8-ready?
234 write-char
235 write-string
236 newline
237 eof-object
238 eof-object?
239 call-with-port
241 ;; Parameters
242 make-parameter
243 parameterize
245 ;; System
246 features
248 ;; Syntax
249 define
250 define-syntax
251 define-record-type
252 lambda
253 if
254 cond
255 case
256 and
257 or
258 when
259 unless
260 let
261 let*
262 letrec
263 letrec*
264 begin
265 do
266 set!
267 quote
268 quasiquote
269 unquote
270 unquote-splicing
271 syntax-rules)
273 ;; Most bindings come from the prelude and native builtins.
274 ;; Module-scoped bindings are re-exported from (sigil math), (sigil string),
275 ;; (sigil io), and (sigil core).
277 (begin
278 ;; R7RS requires multi-list map and for-each.
279 ;; The prelude provides single-list versions, so we wrap list-map/list-for-each.
280 (define (map proc lst . rest)
281 (apply list-map proc lst rest))
283 (define (for-each proc lst . rest)
284 (apply list-for-each proc lst rest))
286 ;; R7RS real? - in Sigil all numbers are real (no complex numbers)
287 (define real? number?)
289 ;; R7RS boolean=? - test if all arguments are booleans with the same value
290 (define (boolean=? b1 b2 . rest)
291 (and (boolean? b1)
292 (boolean? b2)
293 (eq? b1 b2)
294 (or (null? rest)
295 (let loop ((prev b2) (args rest))
296 (or (null? args)
297 (and (boolean? (car args))
298 (eq? prev (car args))
299 (loop (car args) (cdr args))))))))
301 ;; R7RS symbol=? - test if all arguments are the same symbol
302 (define (symbol=? s1 s2 . rest)
303 (and (symbol? s1)
304 (symbol? s2)
305 (eq? s1 s2)
306 (or (null? rest)
307 (let loop ((prev s2) (args rest))
308 (or (null? args)
309 (and (symbol? (car args))
310 (eq? prev (car args))
311 (loop (car args) (cdr args))))))))
313 ;; R7RS list-tail - return the sublist after skipping k elements
314 (define (list-tail lst k)
315 (if (zero? k)
316 lst
317 (list-tail (cdr lst) (- k 1))))
319 ;; R7RS list-set! - mutate the k-th element of a list
320 (define (list-set! lst k val)
321 (set-car! (list-tail lst k) val))
323 ;; R7RS list-copy - shallow copy of a list
324 (define (list-copy lst)
325 (if (pair? lst)
326 (cons (car lst) (list-copy (cdr lst)))
327 lst))
329 ;; R7RS input-port-open? / output-port-open?
330 (define (input-port-open? port)
331 (and (input-port? port) (port-open? port)))
333 (define (output-port-open? port)
334 (and (output-port? port) (port-open? port)))))