base.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ;;; base.scm --- The R6RS base library
  2. ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (library (rnrs base (6))
  18. (export boolean? symbol? char? vector? null? pair? number? string? procedure?
  19. define define-syntax syntax-rules lambda let let* let-values
  20. let*-values letrec letrec* begin
  21. quote lambda if set! cond case
  22. or and not
  23. eqv? equal? eq?
  24. + - * / max min abs numerator denominator gcd lcm floor ceiling
  25. truncate round rationalize real-part imag-part make-rectangular angle
  26. div mod div-and-mod div0 mod0 div0-and-mod0
  27. expt exact-integer-sqrt sqrt exp log sin cos tan asin acos atan
  28. make-polar magnitude angle
  29. complex? real? rational? integer? exact? inexact? real-valued?
  30. rational-valued? integer-valued? zero? positive? negative? odd? even?
  31. nan? finite? infinite?
  32. exact inexact = < > <= >=
  33. number->string string->number
  34. boolean=?
  35. cons car cdr caar cadr cdar cddr caaar caadr cadar cdaar caddr cdadr
  36. cddar cdddr caaaar caaadr caadar cadaar cdaaar cddaar cdadar cdaadr
  37. cadadr caaddr caddar cadddr cdaddr cddadr cdddar cddddr
  38. list? list length append reverse list-tail list-ref map for-each
  39. symbol->string string->symbol symbol=?
  40. char->integer integer->char char=? char<? char>? char<=? char>=?
  41. make-string string string-length string-ref string=? string<? string>?
  42. string<=? string>=? substring string-append string->list list->string
  43. string-for-each string-copy
  44. vector? make-vector vector vector-length vector-ref vector-set!
  45. vector->list list->vector vector-fill! vector-map vector-for-each
  46. error assertion-violation assert
  47. call-with-current-continuation call/cc call-with-values dynamic-wind
  48. values apply
  49. quasiquote unquote unquote-splicing
  50. let-syntax letrec-syntax
  51. syntax-rules identifier-syntax)
  52. (import (rename (except (guile) error raise map)
  53. (log log-internal)
  54. (euclidean-quotient div)
  55. (euclidean-remainder mod)
  56. (euclidean/ div-and-mod)
  57. (centered-quotient div0)
  58. (centered-remainder mod0)
  59. (centered/ div0-and-mod0)
  60. (inf? infinite?)
  61. (exact->inexact inexact)
  62. (inexact->exact exact))
  63. (srfi srfi-11))
  64. (define map
  65. (case-lambda
  66. ((f l)
  67. (let map1 ((hare l) (tortoise l) (move? #f) (out '()))
  68. (if (pair? hare)
  69. (if move?
  70. (if (eq? tortoise hare)
  71. (scm-error 'wrong-type-arg "map" "Circular list: ~S"
  72. (list l) #f)
  73. (map1 (cdr hare) (cdr tortoise) #f
  74. (cons (f (car hare)) out)))
  75. (map1 (cdr hare) tortoise #t
  76. (cons (f (car hare)) out)))
  77. (if (null? hare)
  78. (reverse out)
  79. (scm-error 'wrong-type-arg "map" "Not a list: ~S"
  80. (list l) #f)))))
  81. ((f l1 l2)
  82. (let map2 ((h1 l1) (h2 l2) (t1 l1) (t2 l2) (move? #f) (out '()))
  83. (cond
  84. ((pair? h1)
  85. (cond
  86. ((not (pair? h2))
  87. (scm-error 'wrong-type-arg "map"
  88. (if (list? h2)
  89. "List of wrong length: ~S"
  90. "Not a list: ~S")
  91. (list l2) #f))
  92. ((not move?)
  93. (map2 (cdr h1) (cdr h2) t1 t2 #t
  94. (cons (f (car h1) (car h2)) out)))
  95. ((eq? t1 h1)
  96. (scm-error 'wrong-type-arg "map" "Circular list: ~S"
  97. (list l1) #f))
  98. ((eq? t2 h2)
  99. (scm-error 'wrong-type-arg "map" "Circular list: ~S"
  100. (list l2) #f))
  101. (else
  102. (map2 (cdr h1) (cdr h2) (cdr t1) (cdr t2) #f
  103. (cons (f (car h1) (car h2)) out)))))
  104. ((and (null? h1) (null? h2))
  105. (reverse out))
  106. ((null? h1)
  107. (scm-error 'wrong-type-arg "map"
  108. (if (list? h2)
  109. "List of wrong length: ~S"
  110. "Not a list: ~S")
  111. (list l2) #f))
  112. (else
  113. (scm-error 'wrong-type-arg "map"
  114. "Not a list: ~S"
  115. (list l1) #f)))))
  116. ((f l1 . rest)
  117. (let ((len (length l1)))
  118. (let mapn ((rest rest))
  119. (or (null? rest)
  120. (if (= (length (car rest)) len)
  121. (mapn (cdr rest))
  122. (scm-error 'wrong-type-arg "map" "List of wrong length: ~S"
  123. (list (car rest)) #f)))))
  124. (let mapn ((l1 l1) (rest rest) (out '()))
  125. (if (null? l1)
  126. (reverse out)
  127. (mapn (cdr l1) (map cdr rest)
  128. (cons (apply f (car l1) (map car rest)) out)))))))
  129. (define log
  130. (case-lambda
  131. ((n)
  132. (log-internal n))
  133. ((n base)
  134. (/ (log n)
  135. (log base)))))
  136. (define (boolean=? . bools)
  137. (define (boolean=?-internal lst last)
  138. (or (null? lst)
  139. (let ((bool (car lst)))
  140. (and (eqv? bool last) (boolean=?-internal (cdr lst) bool)))))
  141. (or (null? bools)
  142. (let ((bool (car bools)))
  143. (and (boolean? bool) (boolean=?-internal (cdr bools) bool)))))
  144. (define (symbol=? . syms)
  145. (define (symbol=?-internal lst last)
  146. (or (null? lst)
  147. (let ((sym (car lst)))
  148. (and (eq? sym last) (symbol=?-internal (cdr lst) sym)))))
  149. (or (null? syms)
  150. (let ((sym (car syms)))
  151. (and (symbol? sym) (symbol=?-internal (cdr syms) sym)))))
  152. (define (real-valued? x)
  153. (and (complex? x)
  154. (zero? (imag-part x))))
  155. (define (rational-valued? x)
  156. (and (real-valued? x)
  157. (rational? (real-part x))))
  158. (define (integer-valued? x)
  159. (and (rational-valued? x)
  160. (= x (floor (real-part x)))))
  161. (define (vector-for-each proc . vecs)
  162. (apply for-each (cons proc (map vector->list vecs))))
  163. (define (vector-map proc . vecs)
  164. (list->vector (apply map (cons proc (map vector->list vecs)))))
  165. (define-syntax define-proxy
  166. (syntax-rules (@)
  167. ;; Define BINDING to point to (@ MODULE ORIGINAL). This hack is to
  168. ;; make sure MODULE is loaded lazily, at run-time, when BINDING is
  169. ;; encountered, rather than being loaded while compiling and
  170. ;; loading (rnrs base).
  171. ;; This avoids circular dependencies among modules and makes
  172. ;; (rnrs base) more lightweight.
  173. ((_ binding (@ module original))
  174. (define-syntax binding
  175. (identifier-syntax
  176. (module-ref (resolve-interface 'module) 'original))))))
  177. (define-proxy raise
  178. (@ (rnrs exceptions) raise))
  179. (define-proxy condition
  180. (@ (rnrs conditions) condition))
  181. (define-proxy make-error
  182. (@ (rnrs conditions) make-error))
  183. (define-proxy make-assertion-violation
  184. (@ (rnrs conditions) make-assertion-violation))
  185. (define-proxy make-who-condition
  186. (@ (rnrs conditions) make-who-condition))
  187. (define-proxy make-message-condition
  188. (@ (rnrs conditions) make-message-condition))
  189. (define-proxy make-irritants-condition
  190. (@ (rnrs conditions) make-irritants-condition))
  191. (define (error who message . irritants)
  192. (raise (apply condition
  193. (append (list (make-error))
  194. (if who (list (make-who-condition who)) '())
  195. (list (make-message-condition message)
  196. (make-irritants-condition irritants))))))
  197. (define (assertion-violation who message . irritants)
  198. (raise (apply condition
  199. (append (list (make-assertion-violation))
  200. (if who (list (make-who-condition who)) '())
  201. (list (make-message-condition message)
  202. (make-irritants-condition irritants))))))
  203. (define-syntax assert
  204. (syntax-rules ()
  205. ((_ expression)
  206. (or expression
  207. (raise (condition
  208. (make-assertion-violation)
  209. (make-message-condition
  210. (format #f "assertion failed: ~s" 'expression))))))))
  211. )