big-util.scm 4.3 KB

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