scheme48.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Robert Ransom
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/big/big-util.scm
  8. ;;; scheme48-1.9.2/scheme/rts/exception.scm
  9. (define-module (prescheme scheme48)
  10. #:use-module (ice-9 format)
  11. #:use-module (ice-9 textual-ports)
  12. #:use-module (srfi srfi-8)
  13. #:use-module (srfi srfi-60)
  14. #:use-module (rnrs bytevectors)
  15. #:use-module (rnrs io ports)
  16. #:use-module (prescheme s48-defenum)
  17. #:export (arithmetic-shift
  18. ascii->char
  19. char->ascii
  20. unspecific
  21. make-code-vector
  22. code-vector-ref
  23. code-vector-set!
  24. code-vector-length
  25. make-table
  26. make-symbol-table
  27. table-ref
  28. table-set!
  29. table-walk
  30. byte-ready?
  31. peek-byte
  32. read-byte
  33. write-byte
  34. current-column
  35. current-line
  36. make-tracking-input-port
  37. make-tracking-output-port
  38. assertion-violation
  39. concatenate-symbol
  40. breakpoint
  41. atom?
  42. neq?
  43. n=
  44. memq?
  45. first
  46. any
  47. no-op
  48. null-list?
  49. any?
  50. every?
  51. filter-map
  52. partition-list)
  53. #:re-export (define-enumeration
  54. enum
  55. name->enumerand
  56. enumerand->name
  57. bitwise-and
  58. bitwise-ior
  59. bitwise-xor
  60. bitwise-not
  61. receive))
  62. (define arithmetic-shift ash)
  63. (define ascii->char integer->char)
  64. (define char->ascii char->integer)
  65. (define unspecific (if #f #f))
  66. (define make-code-vector make-bytevector)
  67. (define code-vector-ref bytevector-u8-ref)
  68. (define code-vector-set! bytevector-u8-set!)
  69. (define code-vector-length bytevector-length)
  70. (define make-table make-hash-table)
  71. (define make-symbol-table make-hash-table)
  72. (define table-ref hash-ref)
  73. (define table-set! hash-set!)
  74. (define table-walk hash-for-each)
  75. (define byte-ready? char-ready?)
  76. (define peek-byte lookahead-u8)
  77. (define read-byte get-u8)
  78. (define write-byte put-u8)
  79. (define current-column port-column)
  80. (define current-line port-line)
  81. (define make-tracking-input-port identity)
  82. (define make-tracking-output-port identity)
  83. (define (assertion-violation who message . irritants)
  84. (apply error message irritants))
  85. (define (concatenate-symbol . stuff)
  86. (string->symbol
  87. (apply string-append
  88. (map (lambda (x)
  89. (cond ((string? x) x)
  90. ((symbol? x) (symbol->string x))
  91. ((number? x) (number->string x))
  92. (else
  93. (assertion-violation 'concatenate-symbol "cannot coerce to a string"
  94. x))))
  95. stuff))))
  96. (define (breakpoint format-string . args)
  97. (error (apply format (cons #f (cons format-string args)))))
  98. (define (atom? x)
  99. (not (pair? x)))
  100. (define (neq? a b)
  101. (not (eq? a b)))
  102. (define (n= x y)
  103. (not (= x y)))
  104. (define (memq? x l)
  105. (let loop ((l l))
  106. (cond ((null? l) #f)
  107. ((eq? x (car l)) #t)
  108. (else (loop (cdr l))))))
  109. (define (first pred list)
  110. (let loop ((list list))
  111. (cond ((null? list)
  112. #f)
  113. ((pred (car list))
  114. (car list))
  115. (else
  116. (loop (cdr list))))))
  117. (define any first) ;; ANY need not search in order, but it does anyway
  118. (define (no-op x) x)
  119. (define (null-list? x)
  120. (cond ((null? x) #t)
  121. ((pair? x) #f)
  122. (else
  123. (assertion-violation 'null-list? "non-list" x))))
  124. (define (any? proc list)
  125. (let loop ((list list))
  126. (cond ((null? list)
  127. #f)
  128. ((proc (car list))
  129. #t)
  130. (else
  131. (loop (cdr list))))))
  132. (define (every? pred list)
  133. (let loop ((list list))
  134. (cond ((null? list)
  135. #t)
  136. ((pred (car list))
  137. (loop (cdr list)))
  138. (else
  139. #f))))
  140. (define (filter-map f l)
  141. (let loop ((l l) (r '()))
  142. (cond ((null? l)
  143. (reverse r))
  144. ((f (car l))
  145. => (lambda (x)
  146. (loop (cdr l) (cons x r))))
  147. (else
  148. (loop (cdr l) r)))))
  149. (define (partition-list pred l)
  150. (let loop ((l l) (yes '()) (no '()))
  151. (cond ((null? l)
  152. (values (reverse yes) (reverse no)))
  153. ((pred (car l))
  154. (loop (cdr l) (cons (car l) yes) no))
  155. (else
  156. (loop (cdr l) yes (cons (car l) no))))))