1 ;;; base.scm --- The R6RS base library
3 ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (library (rnrs base (6))
21 (export boolean? symbol? char? vector? null? pair? number? string? procedure?
23 define define-syntax syntax-rules lambda let let* let-values
24 let*-values letrec letrec* begin
26 quote lambda if set! cond case
32 + - * / max min abs numerator denominator gcd lcm floor ceiling
33 truncate round rationalize real-part imag-part make-rectangular angle
34 div mod div-and-mod div0 mod0 div0-and-mod0
36 expt exact-integer-sqrt sqrt exp log sin cos tan asin acos atan
37 make-polar magnitude angle
39 complex? real? rational? integer? exact? inexact? real-valued?
40 rational-valued? integer-valued? zero? positive? negative? odd? even?
41 nan? finite? infinite?
43 exact inexact = < > <= >=
45 number->string string->number
49 cons car cdr caar cadr cdar cddr caaar caadr cadar cdaar caddr cdadr
50 cddar cdddr caaaar caaadr caadar cadaar cdaaar cddaar cdadar cdaadr
51 cadadr caaddr caddar cadddr cdaddr cddadr cdddar cddddr
53 list? list length append reverse list-tail list-ref map for-each
55 symbol->string string->symbol symbol=?
57 char->integer integer->char char=? char<? char>? char<=? char>=?
59 make-string string string-length string-ref string=? string<? string>?
60 string<=? string>=? substring string-append string->list list->string
61 string-for-each string-copy
63 vector? make-vector vector vector-length vector-ref vector-set!
64 vector->list list->vector vector-fill! vector-map vector-for-each
66 error assertion-violation assert
68 call-with-current-continuation call/cc call-with-values dynamic-wind
71 quasiquote unquote unquote-splicing
73 let-syntax letrec-syntax
75 syntax-rules identifier-syntax)
76 (import (rename (except (guile) error raise)
78 (euclidean-quotient div)
79 (euclidean-remainder mod)
80 (euclidean/ div-and-mod)
81 (centered-quotient div0)
82 (centered-remainder mod0)
83 (centered/ div0-and-mod0)
85 (exact->inexact inexact)
86 (inexact->exact exact))
97 (define (boolean=? . bools)
98 (define (boolean=?-internal lst last)
100 (let ((bool (car lst)))
101 (and (eqv? bool last) (boolean=?-internal (cdr lst) bool)))))
103 (let ((bool (car bools)))
104 (and (boolean? bool) (boolean=?-internal (cdr bools) bool)))))
106 (define (symbol=? . syms)
107 (define (symbol=?-internal lst last)
109 (let ((sym (car lst)))
110 (and (eq? sym last) (symbol=?-internal (cdr lst) sym)))))
112 (let ((sym (car syms)))
113 (and (symbol? sym) (symbol=?-internal (cdr syms) sym)))))
115 (define (real-valued? x)
117 (zero? (imag-part x))))
119 (define (rational-valued? x)
120 (and (real-valued? x)
121 (rational? (real-part x))))
123 (define (integer-valued? x)
124 (and (rational-valued? x)
125 (= x (floor (real-part x)))))
127 (define (vector-for-each proc . vecs)
128 (apply for-each (cons proc (map vector->list vecs))))
129 (define (vector-map proc . vecs)
130 (list->vector (apply map (cons proc (map vector->list vecs)))))
132 (define-syntax define-proxy
134 ;; Define BINDING to point to (@ MODULE ORIGINAL). This hack is to
135 ;; make sure MODULE is loaded lazily, at run-time, when BINDING is
136 ;; encountered, rather than being loaded while compiling and
137 ;; loading (rnrs base).
138 ;; This avoids circular dependencies among modules and makes
139 ;; (rnrs base) more lightweight.
140 ((_ binding (@ module original))
141 (define-syntax binding
143 (module-ref (resolve-interface 'module) 'original))))))
146 (@ (rnrs exceptions) raise))
148 (define-proxy condition
149 (@ (rnrs conditions) condition))
150 (define-proxy make-error
151 (@ (rnrs conditions) make-error))
152 (define-proxy make-assertion-violation
153 (@ (rnrs conditions) make-assertion-violation))
154 (define-proxy make-who-condition
155 (@ (rnrs conditions) make-who-condition))
156 (define-proxy make-message-condition
157 (@ (rnrs conditions) make-message-condition))
158 (define-proxy make-irritants-condition
159 (@ (rnrs conditions) make-irritants-condition))
161 (define (error who message . irritants)
162 (raise (apply condition
163 (append (list (make-error))
164 (if who (list (make-who-condition who)) '())
165 (list (make-message-condition message)
166 (make-irritants-condition irritants))))))
168 (define (assertion-violation who message . irritants)
169 (raise (apply condition
170 (append (list (make-assertion-violation))
171 (if who (list (make-who-condition who)) '())
172 (list (make-message-condition message)
173 (make-irritants-condition irritants))))))
175 (define-syntax assert
180 (make-assertion-violation)
181 (make-message-condition
182 (format #f "assertion failed: ~s" 'expression))))))))