big-util.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. (define (concatenate-symbol . stuff)
  3. (string->symbol
  4. (apply string-append
  5. (map (lambda (x)
  6. (cond ((string? x) x)
  7. ((symbol? x) (symbol->string x))
  8. ((number? x) (number->string x))
  9. (else
  10. (error "cannot coerce ~S to a string" x))))
  11. stuff))))
  12. (define (error format-string . args)
  13. (if #t ; work around a bug in the type system
  14. (rts-error (apply format (cons #f (cons format-string args))))))
  15. (define (breakpoint format-string . args)
  16. (rts-breakpoint (apply format (cons #f (cons format-string args)))))
  17. (define (atom? x)
  18. (not (pair? x)))
  19. (define (neq? x y)
  20. (not (eq? x y)))
  21. (define (n= x y)
  22. (not (= x y)))
  23. (define (identity x) x)
  24. (define (no-op x) x) ; guaranteed not to be in-lined
  25. (define (null-list? x)
  26. (cond ((null? x) #t)
  27. ((pair? x) #f)
  28. (else
  29. (error "null-list? got a non-list" x))))
  30. (define (reverse! l)
  31. (cond ((or (null? l)
  32. (null? (cdr l)))
  33. l)
  34. (else
  35. (let ((rest (cdr l)))
  36. (set-cdr! l '())
  37. (let loop ((l1 l) (l2 rest))
  38. (cond ((null? l2)
  39. l1)
  40. (else
  41. (let ((rest (cdr l2)))
  42. (set-cdr! l2 l1)
  43. (loop l2 rest)))))))))
  44. (define (memq? x l)
  45. (let loop ((l l))
  46. (cond ((null? l) #f)
  47. ((eq? x (car l)) #t)
  48. (else (loop (cdr l))))))
  49. (define (first pred list)
  50. (let loop ((list list))
  51. (cond ((null? list)
  52. #f)
  53. ((pred (car list))
  54. (car list))
  55. (else
  56. (loop (cdr list))))))
  57. (define any first) ; ANY? need not search in order, but it does anyway
  58. (define (any? proc list)
  59. (let loop ((list list))
  60. (cond ((null? list)
  61. #f)
  62. ((proc (car list))
  63. #t)
  64. (else
  65. (loop (cdr list))))))
  66. (define (every? pred list)
  67. (let loop ((list list))
  68. (cond ((null? list)
  69. #t)
  70. ((pred (car list))
  71. (loop (cdr list)))
  72. (else
  73. #f))))
  74. (define (filter pred l)
  75. (let loop ((l l) (r '()))
  76. (cond ((null? l)
  77. (reverse r))
  78. ((pred (car l))
  79. (loop (cdr l) (cons (car l) r)))
  80. (else
  81. (loop (cdr l) r)))))
  82. (define (filter! pred list)
  83. (let filter! ((list list))
  84. (cond ((null-list? list)
  85. '())
  86. ((pred (car list))
  87. (set-cdr! list (filter! (cdr list))) list)
  88. (else
  89. (filter! (cdr list))))))
  90. (define (filter-map f l)
  91. (let loop ((l l) (r '()))
  92. (cond ((null? l)
  93. (reverse r))
  94. ((f (car l))
  95. => (lambda (x)
  96. (loop (cdr l) (cons x r))))
  97. (else
  98. (loop (cdr l) r)))))
  99. (define (remove-duplicates list)
  100. (do ((list list (cdr list))
  101. (res '() (if (memq? (car list) res)
  102. res
  103. (cons (car list) res))))
  104. ((null-list? list)
  105. res)))
  106. (define (partition-list pred l)
  107. (let loop ((l l) (yes '()) (no '()))
  108. (cond ((null? l)
  109. (values (reverse yes) (reverse no)))
  110. ((pred (car l))
  111. (loop (cdr l) (cons (car l) yes) no))
  112. (else
  113. (loop (cdr l) yes (cons (car l) no))))))
  114. (define (partition-list! pred l)
  115. (let loop ((l l) (yes '()) (no '()))
  116. (cond ((null? l)
  117. (values (reverse! yes) (reverse! no)))
  118. ((pred (car l))
  119. (let ((rest (cdr l)))
  120. (set-cdr! l yes)
  121. (loop rest l no)))
  122. (else
  123. (let ((rest (cdr l)))
  124. (set-cdr! l no)
  125. (loop rest yes l))))))
  126. (define (delq! object list)
  127. (let loop ((list list))
  128. (cond ((null? list)
  129. '())
  130. ((eq? object (car list))
  131. (loop (cdr list)))
  132. (else
  133. (let loop ((next (cdr list)) (prev list))
  134. (cond ((null? next)
  135. list)
  136. ((eq? (car next) object)
  137. (set-cdr! prev (cdr next))
  138. (loop (cdr next) prev))
  139. (else
  140. (loop (cdr next) next))))))))
  141. (define (delq thing list)
  142. (delete (lambda (x) (eq? x thing)) list))
  143. (define (delete pred in-list)
  144. (let loop ((list in-list) (res '()))
  145. (cond ((null? list)
  146. in-list)
  147. ((pred (car list))
  148. (append-reverse! res (delete pred (cdr list))))
  149. (else
  150. (loop (cdr list) (cons (car list) res))))))
  151. (define (append-reverse! l1 l2)
  152. (let loop ((list l1) (res l2))
  153. (cond ((null? list)
  154. res)
  155. (else
  156. (let ((next (cdr list)))
  157. (set-cdr! list res)
  158. (loop next list))))))
  159. ; Copying strings.
  160. (define (string->immutable-string string)
  161. (if (immutable? string)
  162. string
  163. (let ((copy (string-copy string)))
  164. (make-immutable! copy)
  165. copy)))