AtlatestRepositorysigil-r7rs
sigil-r7rs / tree / src / schemebase.sgl
1
;;; (scheme base) - R7RS Base Library2
;;;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.147
;;;8
;;; For Sigil-specific extensions, use `(sigil core)` instead.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
(export18
;; Equivalence predicates19
eq?20
eqv?21
equal?23
;; Booleans24
not25
boolean?26
boolean=?28
;; Pairs and lists29
pair?30
cons31
car32
cdr33
set-car!34
set-cdr!35
caar cadr cdar cddr36
null?37
list?38
list39
make-list40
length41
append42
reverse43
list-ref44
list-tail45
list-set!46
memq47
memv48
member49
assq50
assv51
assoc52
list-copy53
map54
for-each56
;; Symbols57
symbol?58
symbol=?59
symbol->string60
string->symbol62
;; Numbers63
number?64
integer?65
exact-integer?66
real?67
exact?68
inexact?69
exact70
inexact71
=72
<73
>74
<=75
>=76
zero?77
positive?78
negative?79
odd?80
even?81
max82
min83
+84
-85
*86
/87
abs88
floor89
ceiling90
truncate91
round92
quotient93
remainder94
modulo95
gcd96
lcm97
expt98
square99
floor-quotient100
floor-remainder101
floor/102
truncate-quotient103
truncate-remainder104
truncate/105
number->string106
string->number108
;; Characters109
char?110
char=?111
char<?112
char>?113
char<=?114
char>=?115
char->integer116
integer->char118
;; Strings119
string?120
make-string121
string122
string-length123
string-ref124
string-set!125
string=?126
string<?127
string>?128
string<=?129
string>=?130
substring131
string-append132
string-copy133
string-copy!134
string-fill!135
string-upcase136
string-downcase137
string-foldcase138
string->list139
list->string140
string-for-each141
string-map142
vector->string143
string->vector145
;; Vectors146
vector?147
make-vector148
vector149
vector-length150
vector-ref151
vector-set!152
vector->list153
list->vector154
vector-copy155
vector-copy!156
vector-fill!157
vector-for-each158
vector-map160
;; Bytevectors161
bytevector?162
make-bytevector163
bytevector164
bytevector-length165
bytevector-u8-ref166
bytevector-u8-set!167
bytevector-copy168
bytevector-copy!169
bytevector-append170
utf8->string171
string->utf8173
;; Control features174
procedure?175
apply176
call-with-current-continuation177
call/cc178
values179
call-with-values180
dynamic-wind182
;; Multiple values183
let-values184
let*-values185
define-values187
;; Exceptions188
error189
error-object?190
error-object-message191
error-object-irritants192
error-object-type193
file-error?194
read-error?195
raise196
raise-continuable197
guard198
with-exception-handler200
;; I/O201
input-port?202
output-port?203
textual-port?204
binary-port?205
port?206
current-input-port207
current-output-port208
current-error-port209
close-port210
close-input-port211
close-output-port212
port-open?213
input-port-open?214
output-port-open?215
open-input-string216
open-output-string217
get-output-string218
flush-output-port219
read-char220
peek-char221
read-line222
read-string223
read-u8224
peek-u8225
write-u8226
read-bytevector227
read-bytevector!228
write-bytevector229
open-input-bytevector230
open-output-bytevector231
get-output-bytevector232
char-ready?233
u8-ready?234
write-char235
write-string236
newline237
eof-object238
eof-object?239
call-with-port241
;; Parameters242
make-parameter243
parameterize245
;; System246
features248
;; Syntax249
define250
define-syntax251
define-record-type252
lambda253
if254
cond255
case256
and257
or258
when259
unless260
let261
let*262
letrec263
letrec*264
begin265
do266
set!267
quote268
quasiquote269
unquote270
unquote-splicing271
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
(begin278
;; 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 value290
(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 symbol302
(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 elements314
(define (list-tail lst k)315
(if (zero? k)316
lst317
(list-tail (cdr lst) (- k 1))))319
;; R7RS list-set! - mutate the k-th element of a list320
(define (list-set! lst k val)321
(set-car! (list-tail lst k) val))323
;; R7RS list-copy - shallow copy of a list324
(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)))))